Example 1 — null and undefined
Loose equality treats them as equal.
JavaScript
console.log(null == undefined);
console.log(null === undefined); How It Works
- == special-cases null/undefined.
- === keeps them different.
Use === (strict) to compare value and type with no coercion. == (loose) converts types before comparing and surprises beginners—prefer === daily.
console.log(1 === "1");
console.log(1 == "1");
console.log(NaN === NaN); Loose equality treats them as equal.
console.log(null == undefined);
console.log(null === undefined); Different objects are not ===.
console.log({} === {});
const a = {}; console.log(a === a); Like === but Object.is(NaN, NaN) is true.
console.log(Object.is(NaN, NaN));
console.log(Object.is(+0, -0)); CODETOFUN NEWS
Get the latest IT news, programming updates, and technology trends directly on your phone.
Your daily dose of tech, straight to your feed.
6 people found this page helpful