An if
statement is used to make decisions in code based on a condition. Here's the basic syntax for an if
statement in JavaScript:
index.tsx68 chars4 lines
The condition
in the parentheses is evaluated to either true
or false
. If it's true
, the code inside the curly braces is executed; if it's false
, the code is skipped.
Here's an example of an if
statement in action:
index.tsx67 chars6 lines
In this example, the condition x > 5
evaluates to true
since the value of x
is 10
. The code inside the if
statement's curly braces (console.log("x is greater than 5")
) is executed and the message "x is greater than 5" is printed to the console.
gistlibby LogSnag