== converts before comparing, === does not. And every value counts as true or false in an if: see which ones are falsy.
JavaScript · beginner · 6 min · 16 steps · free
const a = 0;
const b = '0';
console.log(a == b);
console.log(a === b);
const values = [0, '', 'hello', null, []];
for (const v of values) {
if (v) {
console.log(v, 'is truthy');
} else {
console.log(v, 'is falsy');
}
}a is created with the value 0.b is created with the value "0".0 == "0" is true: == converts the string to a number before comparing.0 === "0" is false: a number and a string are never strictly equal.values is created and points to a new Array: [0, "", "hello", null, an Array].v begins.0 is falsy.v is "", which is falsy, so the if-block is skipped. is falsy.v is "hello", which is truthy, so the if-block runs.hello is truthy.v is null, which is falsy, so the if-block is skipped.null is falsy.[] is truthy.Answer: false. Different types.
Answer: Yes. "0" is a non-empty string, so it is truthy.
← Values and Types · Strings and Template Literals →