Skip to main content
Back to Technical Articles
December 27, 2020#javascript#conditional statements

Conditional Statements in JavaScript

In JavaScript, Conditional Statements executes certain operations based on logic.

Conditional Statements execute certain operations based on logic. In Javascript, we can execute the conditional statements in three (3) ways as below:

  • ternary operator
  • if / else logic
  • switch statement

let suppose, we have a variable in Javascript as below:

let isMember = true;

We can perform condition statements in javascript as below:

ternary operator:

// ternary operator
let price= isMember ? '$2.00' : '$10.00'

if / else logic:

// if / else logic
if(isMember){
  price = '$2.00'
}else{
  price = '$10.00'
}

switch statement:

// switch statement
switch(isMember) {
  case true:
    price = '$2.00';
    break;
  case y:
    price = '$10.00'
    break;
  default:
    price = '$2.00';
}
Share this article
Taimoor Sattar • Engineering Blog

If you found this technical guide helpful, explore full-stack web development tutorials, Sanity CMS dataset management, and Stripe integration on the course page.