Type Conversion and Coercion
JavaScript is a dynamically typed language. A variable can hold a string at one moment and a number later.
let value = "42";
value = 42;This flexibility is useful, but it also means JavaScript sometimes changes values from one type to another.
There are two important terms:
- Type conversion: You explicitly change a value from one type to another.
- Type coercion: JavaScript automatically changes a value behind the scenes.
Understanding the difference helps you avoid confusing bugs.
Type Conversion
Type conversion is explicit.
You, the developer, ask JavaScript to convert a value.
const ageText = "25";
const age = Number(ageText);
console.log(age); // 25
console.log(typeof age); // numberThis is usually safer than relying on JavaScript to guess what you meant.
Converting to a String
Use String() to convert a value to a string.
console.log(String(42)); // "42"
console.log(String(true)); // "true"
console.log(String(null)); // "null"
console.log(String(undefined)); // "undefined"Some values also have a .toString() method.
const count = 42;
console.log(count.toString()); // "42"String() is often safer because it works with null and undefined.
console.log(String(null)); // "null"
// null.toString(); // ErrorConverting to a Number
Use Number() to convert a value to a number.
console.log(Number("100")); // 100
console.log(Number("3.14")); // 3.14
console.log(Number(true)); // 1
console.log(Number(false)); // 0
console.log(Number(null)); // 0
console.log(Number(undefined)); // NaNIf JavaScript cannot convert the value into a valid number, the result is NaN.
console.log(Number("hello")); // NaNAn empty string becomes 0:
console.log(Number("")); // 0This is one reason conversions should be done intentionally and tested carefully.
parseInt() and parseFloat()
Number() expects the whole value to be numeric.
console.log(Number("100px")); // NaNNumber.parseInt() can read an integer from the beginning of a string.
console.log(Number.parseInt("100px")); // 100
console.log(Number.parseInt("42")); // 42Number.parseFloat() can read a decimal number.
console.log(Number.parseFloat("3.14px")); // 3.14
console.log(Number.parseFloat("9.99")); // 9.99These functions are useful when extracting numbers from text, such as CSS values.
Converting to a Boolean
Use Boolean() to convert a value to true or false.
console.log(Boolean("Hello")); // true
console.log(Boolean("")); // false
console.log(Boolean(42)); // true
console.log(Boolean(0)); // false
console.log(Boolean(null)); // false
console.log(Boolean(undefined)); // false
console.log(Boolean(NaN)); // falseThis uses JavaScript's truthy and falsy rules.
Falsy values include:
false;
0;
"";
null;
undefined;
NaN;Most other values are truthy.
console.log(Boolean("0")); // true
console.log(Boolean("false")); // true
console.log(Boolean([])); // true
console.log(Boolean({})); // trueType Coercion
Type coercion is implicit.
JavaScript automatically converts a value because of the operation you are performing.
This can be convenient, but it can also be surprising.
console.log("5" + 1); // "51"
console.log("5" - 1); // 4The first result is a string. The second result is a number.
To write predictable code, you should understand why.
String Coercion with +
The + operator has two jobs in JavaScript:
- Add numbers
- Join strings
If one side is a string, JavaScript usually converts the other side to a string and concatenates.
console.log("5" + 1); // "51"
console.log("Hello " + "World"); // "Hello World"
console.log(true + " value"); // "true value"This is why template literals are often clearer.
const count = 5;
console.log(`Count: ${count}`);Number Coercion with Math Operators
Operators like -, *, /, and % are only mathematical operators.
So JavaScript tries to convert string values to numbers.
console.log("10" - 2); // 8
console.log("10" * "2"); // 20
console.log("10" / 2); // 5
console.log("10" % 3); // 1If conversion fails, the result is NaN.
console.log("hello" - 2); // NaNBoolean Coercion in Conditions
Conditions automatically convert values to booleans.
const name = "Asha";
if (name) {
console.log("Name exists");
}The string "Asha" is truthy, so the if block runs.
An empty string is falsy:
const name = "";
if (name) {
console.log("Name exists");
} else {
console.log("Name is empty");
}This behavior is common and useful, but be careful when values like 0 are valid.
const count = 0;
if (count) {
console.log("Count exists");
} else {
console.log("This also runs when count is zero");
}If 0 is a valid value, check more directly.
if (count !== null && count !== undefined) {
console.log("Count has a value");
}Loose Equality Coercion
The == operator allows coercion before comparing.
console.log(5 == "5"); // true
console.log(0 == false); // true
console.log("" == 0); // true
console.log(null == undefined); // trueThese results can be surprising.
The === operator checks both value and type.
console.log(5 === "5"); // false
console.log(0 === false); // false
console.log("" === 0); // false
console.log(null === undefined); // falseUse strict equality by default.
The null == undefined Exception
There is one common intentional use of loose equality:
if (value == null) {
console.log("Value is null or undefined");
}This catches only null and undefined, not other falsy values.
console.log(null == undefined); // true
console.log(0 == null); // false
console.log("" == null); // false
console.log(false == null); // falseUse this pattern only when you specifically want to check for both null and undefined.
Best Practices
- Prefer explicit conversion with
String(),Number(), andBoolean(). - Use
===and!==instead of==and!=in most cases. - Be careful with the
+operator when strings are involved. - Use template literals for readable string building.
- Check for
NaNwithNumber.isNaN(). - Be careful with truthy/falsy checks when
0,"", orfalseare valid values.
Summary
Type conversion and coercion both change values from one type to another, but they happen in different ways.
Remember:
- Type conversion is explicit and controlled by you.
- Type coercion is automatic and done by JavaScript.
String(),Number(), andBoolean()are common conversion tools.+may concatenate strings instead of adding numbers.- Math operators like
-,*, and/usually coerce values to numbers. - Conditions coerce values to booleans.
- Loose equality
==performs coercion and can be surprising. - Strict equality
===avoids hidden type conversion.
You have now completed the Variables and Data Types module. The next step is to test your understanding with the module knowledge checks.