Categories: Javascript Tutorial

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

Admin

Working Professional with more than 12 Years of Experience in Software Testing with Automation and Manual Testing Knowledge.

Recent Posts

Objects, Classes and Constructor in Javascript

Objects, Classes and Constructor in Javascript Classes and Objects A class in any programming language…

55 years ago

OOPs in Javascript

We will start from object oriented programming basics like What are different concepts in Object…

55 years ago

Why we can not over ride Constructor

Why we can not over ride a Constructor This is a very important question, that,…

55 years ago

Javascript Tutorial – Arrow Function in Javascript

What is Arrow Function In the last session, We had discussed about Anonymous Function. Anonymous…

55 years ago

Javascript Tutorial – Anonymous Function in Javascript

What is Anonymous Function Anonymous Function is a type of function which has no name…

55 years ago

Javascript Tutorial – Map, Filter and Reduce

What is Map, Reduce and Filter Map Filter and Reduce are higher order methods for…

55 years ago