Skip to main content

Command Palette

Search for a command to run...

08. BASICS | Conditional Statements in JavaScript

This article will talk about conditional statements in javascript.

Updated
2 min readView as Markdown
S
Full-Stack Developer with 4 years' experience, specializing in backend development. Skilled in JavaScript, React, Python, Databases, and AWS. Known for building scalable web apps, leading teams, and maintaining strong client communication. Upskilling in Generative AI.

if statement

if (condition) {
    // execute this code if condition is true
}

if ... else statement

if (condition) {
    // execute this code if condition is true
} else {
    // execute this code if condition is false
}

if ... else if ... else statement

if (condition1) {
    // execute this code if condition1 met
    // don't check for remaining conditions
} else if (condition2) {
    // execute this code if condition1 doesn't met but condition2 met
    // don't check for remaining conditions
} else if (condition3) {
    // execute this code if condition1 & 2 don't met but condition3 met
    // don't check for remaining conditions
} else if (condition4) {
    // execute this code if condition1, 2 & 3 don't met but condition4 met
} else {
    // execute this code if none of the above conditions met
}

switch statement

switch (expression) {
  case value1:
    // Code to execute if expression === value1
    break;
  case value2:
    // Code to execute if expression === value2
    break;
  // ... more cases ...
  default:
    // Code to execute if none of the cases match
}

ternary operator

Syntax: condition ? expression1 : expression2

If condition is true, execute expression1 else execute expression2.

(marks >= 33) ? "Pass" : "Fail"

Happy reading!