== vs === in JavaScript with Examples
A set of worked examples showing when == and === return true or false for each type combination, with a quick reference table.
466 posts about JavaScript: questions with answers, tutorials and articles, newest first.
A set of worked examples showing when == and === return true or false for each type combination, with a quick reference table.
The === and !== operators compare type and value without coercion. See how they differ from == and != and how to handle NaN, objects and null.
== is safe for two primitive strings but coerces types otherwise, giving results like "0" == 0. Learn why === is preferred and how to compare strings correctly.
The speed difference between == and === in JavaScript is negligible. Use === for predictable results and avoid hidden type coercion bugs.
!== compares type and value with no conversion, while != coerces types first. See examples, a comparison table and when each is acceptable.
!= null checks that a value is neither null nor undefined in JavaScript. See how it differs from !== null and if (value), plus Java, PHP and SQL behaviour.
!== is the strict inequality operator in JavaScript. It compares type and value with no coercion. See how it differs from != with clear examples.
Use === by default: it compares value and type without coercion. Keep == only for the null or undefined check.
Strict equality compares type and value without coercion, so results are predictable. See surprising loose equality cases and how to enforce the rule.
typeof returns "number" for integers, floats, NaN and Infinity. Learn the extra Number checks that make validation reliable, plus BigInt handling.
=== compares both type and value without coercion. See how it treats objects, NaN and zero, and why it is the recommended default.
JavaScript has one number type for integers and decimals. Learn how to declare numbers, convert strings, handle NaN and precision, and use BigInt.
Learn which check to use for each JavaScript data type: typeof, Array.isArray, instanceof or Object.prototype.toString, and avoid the null and NaN traps.
Use typeof for primitives, Array.isArray for arrays, instanceof for class instances and Object.prototype.toString for exact built-in types.
typeof returns object for null, arrays and dates, so use typeof plus a null check, then Array.isArray or a prototype test for specific kinds.
Compare typeof value to "string" to check for strings in JavaScript. Learn why typeof always returns a string, and which edge cases and common mistakes to avoid.
typeof returns one of eight strings in JavaScript. Learn each result, the null and array quirks, and when to use other checks.
typeof returns the string number for every numeric value, including NaN and Infinity. See the edge cases and how to validate numbers correctly.
The syntax is typeof operand, and it returns a string such as number or string. See all return values, the null quirk and better type checks.
typeof is the right check for primitives and undeclared variables, but it misreports null and arrays. See what it returns and which checks to use instead.