If Statement

Example:

if (true) {
	console.log('This message will print.');
}

// Output: This message will print!

In the above block of code:

  • Their is a if keyword followed by a set of parentheses () which is followed by a block of code inside a set of curly braces {}.

  • Inside the parentheses (), a condition is provided that evaluates to true and false.

  • If the condition evaluates to true, the code block inside the curlt braces will run or execute.

  • If the condition evaluates to false, the block wont execute.

Example:

let sale = true;

if (sale) {
  console.log('Time to buy!');
}

sale = false;

if (sale) {
  console.log('Time to buy!');
}

// Output: Time to buy!

Last updated