JavaScript fundamentals / 03
if and else
Write conditional branches, recognize falsy values, and compare loose and strict equality.
On this page
What is a conditional statement?
A conditional statement runs different code depending on whether a condition is true. Two common forms are if and switch.
Basic syntax
The original interactive example checks whether a == b. If so, it runs document.write("a = b"). Otherwise, it checks a + 1 == b and writes "a + 1 = b" if that condition is true. If neither condition is true, it writes "?".
Open the original if/else example on CodePen.
Some values are treated as false in a condition. The original console examples show false, undefined, NaN, null, 0, and an empty string, "". NaN stands for Not a Number and represents an invalid numeric result.

Comparison operators
The operators used in conditions include ===, !==, ==, !=, <, >, <=, and >=.
===checks strict equality without converting different types.!==is its opposite.==uses type conversion when appropriate, so1and'1'can compare equal.!=is the opposite of loose equality.<and>compare whether one value is less than or greater than another.<=and>=include equality in those comparisons.
Loose and strict equality
As noted above, == treats the number 1 and the string '1' as equal after conversion. Strict equality, ===, distinguishes them because their types differ. The second interactive example illustrates the difference:
Open the original equality comparison on CodePen.
Translation note: the source called this final section “the ternary operator,” but its explanation and example concern equality. The heading has been corrected to match the actual content. The screenshot's falsy-value list is introductory rather than exhaustive.
Next, we will look at switch statements.
Moved here from Tistory
I migrated this post from my Korean Tistory blog, I am Jason Lee, to this website and translated it into English. My writing and projects now live together in one place.
The original publication date, code examples, and screenshots are preserved. Editorial notes clarify known issues in the original material.
Original post on Tistory English edition · Aug 30, 2026