207k views
1 vote
How to write an IF statement in JavaScript?

a) if (i == 5)
b) if i == 5 then
c) if i = 5
d) if i = 5 then

User Ninge
by
8.3k points

1 Answer

3 votes

Final answer:

An IF statement in JavaScript is written using the syntax 'if (i == 5)'. It uses double equals '==' for comparison to check if the variable 'i' has a value equal to 5.

Step-by-step explanation:

To write an IF statement in JavaScript, the correct syntax is:

if (i == 5)

This is a conditional statement that checks whether the variable i is equal to the number 5. The double equals sign '==' is used for comparison. Here's an example of the full usage in a script:

if (i == 5) {
console.log('i is 5');
} else {
console.log('i is not 5');
}

In this code block, a message will log to the console stating whether i is 5 or not based on the condition. It is important to use the double equals '==' or triple equals '===' for comparison, not the single equals '=', which is used for assignment.

User Lededje
by
8.2k points