In JavaScript, conditionals statements form different forms - if statement, if..else statements, nested if..else statements, if..else..if chain, and switch..case statements.
Let's start with the if-statement. The syntax of the if statement in JavaScript is:
if (condition) { // code to be executed when the condition holds true }
Example:
var x = 4; if (x % 2 == 0) { console.log("x is even."); }
Output:
"x is even."
if (condition) { // code to be executed when the condition holds true } else { // code to be executed when the condition holds false }
Example:
var x = 5; if (x % 2 == 0) { console.log("x is even."); } else { console.log("x is odd."); }
Output:
"x is odd."