OneBite.Dev - Coding blog in a bite size

use if conditional statement in Javascript

Code snippet on how to use if conditional statement in Javascript

if (x === "good") {
  console.log("I feel great!");
}

This code uses an if conditional statement to check if the value of the variable “x” is equal to the string “good”. If it is, the code will log “I feel great!” to the console. If the value is not equal to “good”, this code will do nothing.

The statement is enclosed in parentheses, and includes the operator ”===”, which is used to test if two values are equal to each other. Inside of the statement, the value being compared is “x”, followed by the ”===”, followed by the string value “good”. The statement is finished by a set of curly brackets {}, which contain the code to be run if the statement is true.

In this case, the code will log the string “I feel great!” to the console, which can be viewed using browser dev tools.

javascript