Conditional Execution in JavaScript

Truthy & Falsy

Conditional execution in JavaScript is dependent on truthy and falsy values. All expressions in JavaScript are truthy unless they are one of the following: 1) false, 2) null, 3) undefined, 4) an empty string, 5) the number zero or 6) NaN.

if

if statement allows for execution of a given block of code only under a specified JavaScript expression is of a truthy value.

if (2 + 2 === 4) {
  console.log('2 and 2 does equal 4.')
} // "2 and 2 does equal 4."

if ... else if

if statement can be extended with else if. The block of code after the else if is executed only when the condition following the if is falsy and the condition following the else if is truthy.

const anArr = [1,2,3]

if (anArr.length === 2) {
  console.log('The array has two items.')
} else if (anArr.length === 3) {
  console.log('The array has threee items.')
} // "The array has threee items."

if ... else

if statement can be extended with else. else is not followed by any condition and the block of code after the else is executed when no condition in if ... else (or if ... else if ... else) statement is truthy.

if (2 + 2 === 5) {
  console.log('2 + 2 does equal 5.')
} else {
  console.log('2 + 2 does not equal 5.')
} // "2 + 2 does not equal 5.."

if ... else if ... else

if statement can be extended with multiple else if and combined with else simultaneously.

switch

Although the statements are not tantamount, in some circumstances in place of if ... else if ... else statement a switch statement can be used. Notice a break keyword is the switch statement.

const otherArr = [1,2,3,4,5]

if (otherArr.length === 2) {
  console.log('The array has two items.')
} else if (otherArr.length === 3) {
  console.log('The array has threee items.')
} else {
  console.log("I don't know how many items the array has.")
} // "I don't know how many items the array has."

switch(otherArr.length) {
  case 2:
    console.log('The array has two items.')
    break
  case 3:
    console.log('The array has threee items.')
    break
  default:
    console.log("I don't know how many items the array has.")
} // "I don't know how many items the array has.

A switch statement checks whether a given expression matches provided cases.

Once a case is matched switch statement executes all statements following the matched case unless a break (or return) keyword is met.

switch uses strict comparison (===) to evaluate whether a given case matches.

Ternary Operator

The ternary operator checks whether a given condition is truthy and depending on that executes and returns the evaluated value of one of two provided expression.

const theAnswer = 2 + 2 === 4 ? '2 + 2 equals 4.' : '2 + 2 does not equal 4.'
console.log(theAnswer) // '2 + 2 does not equal 4.'

We use cookies and similar technologies to enhance the quality of services, maintain statistics and adjust marketing content. You will find more information in the Cookies Policy.

By clicking OK you grant consent to processing of your personal data by us and our Trusted Partners with the purpose of maintain statistics and adjustment of the marketing content pursuant to the Privacy Policy. If you wish to not grant that consent and/or limit its extent click Settings.