Type coercion

Type coercion (type conversion, type casting) is the process of changing an expression from one data type to another.

Implicit type coercion

The JavaScript interpreter, for example, implicitly coerces, or converts, some data types to another in certain expressions, then performs the operation. The rules for coercion of string, number, or boolean values are as follows:

  • If you add a number and a string, the number is coerced to a string.
  • If you add a boolean and a string, the boolean is coerced to a string.
  • If you add a number and a boolean, the boolean is coerced to a number.