JavaScript can change a value from one data type to another. When JavaScript does this on its own, it is called type coercion. When you do it explicitly in your code, it is called type conversion. Understanding both helps avoid bugs and weird console output.
Number("10")).String(), Number(), Boolean(), parseInt(), parseFloat().true or false when converted to boolean.In practice, you will mostly convert values to strings, numbers, or booleans using built-in functions and constructors. Below are some common patterns.
You can convert numbers, booleans, or objects to strings using String() or .toString().
let num = 100;
let str1 = String(num); // "100"
let str2 = (20).toString(); // "20"
let bool = true;
let str3 = String(bool); // "true"
console.log(str1, str2, str3);
You can convert strings and booleans to numbers using Number(), parseInt(), or parseFloat().
let a = "123";
let b = Number(a); // 123
let c = parseInt("45px"); // 45
let d = parseFloat("12.34"); // 12.34
let e = Number(true); // 1
let f = Number(false); // 0
console.log(b, c, d, e, f);
Use Boolean() to convert a value to a boolean. Falsy values like 0, "", null, undefined, and NaN become false. Everything else is generally truthy.
console.log(Boolean(0)); // false
console.log(Boolean("")); // false
console.log(Boolean("hello")); // true
console.log(Boolean(123)); // true
JavaScript sometimes converts types automatically, especially with operators like + and -:
console.log("5" + 1); // "51" (1 is converted to string)
console.log("5" - 1); // 4 ("5" is converted to number)
console.log(true + 1); // 2 (true becomes 1)
console.log(false + "1"); // "false1"
Number() tries to convert the whole string. parseInt() and parseFloat() read as much numeric content as possible from the start of the string.
Number("10.5") → 10.5parseInt("10.5") → 10Number("10px") → NaNparseInt("10px") → 10
console.log(Number("10.5")); // 10.5
console.log(parseInt("10.5")); // 10
console.log(Number("10px")); // NaN
console.log(parseInt("10px")); // 10
From the examples above:
String(100) becomes "100", and String(true) becomes "true".Number("123") gives 123, while Number("10px") gives NaN because of extra text.parseInt("45px") reads the starting number and returns 45.Boolean(0) and Boolean("") are false, but Boolean("hello") is true."5" + 1 results in a string ("51"), while "5" - 1 results in a number (4).These behaviours show why understanding type conversion and coercion is important: the same operator can give different results depending on the types of its operands.
Number() for precise conversion; it returns NaN if the whole string is not a valid number.parseInt() and parseFloat() when you expect extra characters after the numeric part.parseFloat() instead of parseInt() if you need decimal values.Boolean() is useful to quickly check if a value is "truthy" or "falsy".+ with strings: if either operand is a string, the result will be a string concatenation.String() and log it to the console.parseInt() and Number() on the same string (like "50px") and compare the results.Boolean() on different values such as "0", " " (space), [], and {}.