Example 1 — To boolean
Truthy/falsy rules.
JavaScript
console.log(!!"x");
console.log(!!0);How It Works
- !! coerces to boolean.
Type coercion is automatic conversion between types. == coerces before comparing. + prefers string concatenation if either side is a string. Prefer === and explicit conversions in most code.
console.log(1 + "2");
console.log("5" - 2);
console.log(1 == "1");
console.log(1 === "1");Truthy/falsy rules.
console.log(!!"x");
console.log(!!0);Avoid surprises.
console.log(Number(" 8 "));
console.log(Number("x"));valueOf / toString.
console.log(String({}));
console.log(Number([]));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