Because the + operator favors string concatenation. When one operand is a string, the other is coerced to a string, so 1 becomes '1' and the result is '12'.
JavaScript Is Weakly Typed: What That Means
Why does 1 + '2' give '12' in JavaScript?
The answer
More FAQs in JavaScript Is Weakly Typed: What That Means
Variables can hold any type and change types at runtime. The engine coerces values automatically when types do not match, which can lead to unexpected results like 1 + '2' = '12'.
== coerces operands to a common type before comparing, so '0' == 0 is true. === checks both type and value without coercion, so '0' === 0 is false. Always prefer ===.
Dynamically and weakly typed. Types are checked at runtime, not compile time, and the engine coerces values automatically when types mismatch.
Still have questions?
Browse all our FAQs or reach out to our support team
Want to upskill yourself?
Your learning library
Small steps. Big possibilities.
Find your next course. Build something great.