Javascript Tutorial – Conditional Expression in Javascript

Conditional Expression in Javascript

In our last session, We had discussed about Comparison Operators, that they return a true or false value, i.e. boolean value. We might get a question after that, what is the use of boolean values (true or false). One of the use of these boolean values is in Conditional expression.

Now, we might get a question, What is Conditional expression. So, Conditional Expression or Conditional statements are the expressions that depend on boolean value for their execution. When condition is true, Conditional Expression gets executed, but when the condition is false, Conditional Expression doesn’t get executed.Article Sponsored Find something for everyone in our collection of colourful, bright and stylish socks. Buy individually or in bundles to add color to your sock drawer!

Type of Conditional Expression

if

if ….. else

if ….. else if …. else

Syntax and Examples of Conditional Statements

Syntax of if statements

if(condition) {
block of code
}

Example of if statements

if(true) {
console.log("Inside if block")
}

if(false) {
console.log("Inside if block")
}

let i=10
if(i>5) {
console.log("Inside if block")
}

if(i<5) {
console.log("Inside if block")
}

Syntax of if….else statements

if(condition) {
block of code
} else {
block of code
}

Example of if….else statements

if(true) {
console.log("Inside if block")
} else {
console.log("Inside else block")
}

if(false) {
console.log("Inside if block")
} else {
console.log("Inside else block")
}

let i=10
if(i>5) {
console.log("Inside if block")
} else {
console.log("Inside else block")
}

if(i<5) {
console.log("Inside if block")
} else {
console.log("Inside else block")
}

Syntax of if ….. else if …. else statements

if(condition) {
block of code
} else if (condition) {
block of code
} else {
block of code
}

Example of if ….. else if …. else statements

if(true) {
console.log("Inside if block")
} else if (true) {
console.log("Inside else if block")
} else {
console.log("Inside else block")
}

if(false) {
console.log("Inside if block")
} else if (true) {
console.log("Inside else if block")
}else {
console.log("Inside else block")
}

if(false) {
console.log("Inside if block")
} else if (false) {
console.log("Inside else if block")
}else {
console.log("Inside else block")
}

let i=10
if(i>5) {
console.log("Inside if block")
} else if (i===10) {
console.log("Inside else if block")
} else {
console.log("Inside else block")`
}

if(i<10) {
console.log("Inside if block")
} else if (i===10) {
console.log("Inside else if block")
} else {
console.log("Inside else block")`
}

Summary

We have covered below topics in this session today.

  1. Conditional Expression
  2. Types of Conditional Expression
  3. Syntax of Conditional Expression
  4. Examples of Conditional Expression

Leave a Comment