Conditional Statements in JavaScript

Taimoor Sattar

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';
}

About the Author

Written by Taimoor Sattar I'm Taimoor Sattar, full-stack developer. I've experience to develop websites in JavaScript, Node and CSS. But, better I am a problem solver. I used to break down large chunck of problem into small puzzle pieces. You should follow them on Twitter. You can contact me on this page.