if loop in javascript

In JavaScript, the if statement is used to execute a block of code if a specified condition is true. The syntax of the if statement is as follows:

index.tsx
if (condition) {
  // code to be executed if the condition is true
}
69 chars
4 lines

Here is an example usage:

index.tsx
let x = 5;
if (x > 3) {
  console.log("x is greater than 3");
}
64 chars
5 lines

In this example, the condition is x > 3, which is true since x is equal to 5. Therefore, the code inside the curly braces {} is executed and the output would be x is greater than 3.

You can also use else and else if statements to add more conditions:

index.tsx
let time = 15;
if (time < 10) {
  console.log("Good morning");
} else if (time < 20) {
  console.log("Good day");
} else {
  console.log("Good evening");
}
156 chars
9 lines

This code checks the value of time and outputs a different message depending on its value. If time is less than 10, it outputs Good morning. If time is between 10 and 20, it outputs Good day. If time is greater than or equal to 20, it outputs Good evening.

gistlibby LogSnag