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.tsx69 chars4 lines
Here is an example usage:
index.tsx64 chars5 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.tsx156 chars9 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