🍋Implicit Conversion & Falcy Values

Implicit conversion refers to the automatic type coercion that occurs when operators or functions expect a certain type of value, but are given a different type. For example, the addition operator (+) expects numbers as operands, but if one of the operands is a string, JavaScript will try to convert the string to a number before performing the addition. This can lead to some unexpected behavior if you are not careful.

Falsy values are values that evaluate to false when converted to a boolean. There are six falsy values in JavaScript:

  • false

  • 0 (zero)

  • "" (empty string)

  • null

  • undefined

  • NaN (not a number)

All other values, including objects and arrays, are considered truthy. You can use these falsy values to your advantage by using them in conditional statements to check whether a value is "truthy" or "falsy".

For example:

const x = 0;
if (x) {
  console.log('x is truthy');
} else {
  console.log('x is falsy');
}
// Output: x is falsy

const y = 'hello';
if (y) {
  console.log('y is truthy');
} else {
  console.log('y is falsy');
}
// Output: y is truthy

Last updated