Type Conversion in JavaScript
Code Playground is only enabled on larger screen sizes.
Automatic conversion
JavaScript is a weak type programming language. It will always try to execute the code, regardless of the given value's data types, even if it does not make sense.
For example:
console.log(1 + "1"); // -> 11In this case, JavaScript assumes you are trying to perform a concatenation operation, and it will convert the number 1 into a string, and then join the two strings together.
And in the following example, null will be converted into the number 0, and 5 times 0 gives 0.
console.log(5 * null); // -> 0Even though these examples technically work, you should never rely on automatic conversions, as they can lead to unexpected problems.
A better way is to convert the values to the right types explicitly, in order to ensure they are always the intended types.
Convert to strings
For example, if you intend to perform string concatenation, then make sure all the values are converted into strings using the String() function.
console.log(typeof String(null)); // -> string
console.log(typeof String(undefined)); // -> string
console.log(typeof String(true)); // -> string
console.log(typeof String(false)); // -> string
console.log(typeof String(123)); // -> string
console.log(typeof String("123")); // -> stringBoolean values and numbers also have a built-in toString() method that does the same thing.
console.log(typeof true.toString()); // -> string
console.log(typeof false.toString()); // -> string
console.log(typeof (123).toString()); // -> stringThe difference between String() and toString() is that String() is global. It works anywhere and it works on null and undefined as well.
console.log(typeof String(null)); // -> string
console.log(typeof String(undefined)); // -> stringtoString() is tied to specific data types, meaning the toString() for numbers and the toString() for Boolean values are not the same.
And they are not implemented for null and undefined in JavaScript. The following code will cause an error:
console.log(typeof null.toString()); // -> ErrorConvert to numbers
The Number() function can be used to convert other data types into numbers. The conversion obeys the following rules:
- Strings that contain only white spaces will be converted into 0.
console.log(Number("\n\t ")); // -> 0- Strings that can be converted into numbers without errors, meaning they contain only number digits or a sign in the front, will be converted into the corresponding number.
console.log(Number("00001")); // -> 1
console.log(Number("12345")); // -> 12345
console.log(Number("-12345")); // -> -12345
console.log(Number("-0.001")); // -> -0.001- The leading and tailing white spaces will be automatically removed.
console.log(Number("\t00001\t"));
console.log(Number("12345\n"));1
12345- If an error is encountered during conversion, meaning one or more characters cannot be converted, then it will return
NaN.
console.log(Number("000a01")); // -> NaN
console.log(Number("123 45")); // -> NaN
console.log(Number("-12-345")); // -> NaN
console.log(Number("-0.00.1")); // -> NaN- For Boolean values,
truewill become 1, andfalsewill become 0.
console.log(Number(true)); // -> 1
console.log(Number(false)); // -> 0nullwill become 0, andundefinedwill becomeNaN.
console.log(Number(null)); // -> 0
console.log(Number(undefined)); // -> NaNThis is one of the few differences between null and undefined.
Convert to Boolean values
The Boolean() function is used to convert other data types into either true or false.
All falsy values will become false:
console.log(Boolean(null)); // -> false
console.log(Boolean(undefined)); // -> false
console.log(Boolean(0)); // -> false
console.log(Boolean("")); // -> falseAll truthy values will become true:
console.log(Boolean(10)); // -> true
console.log(Boolean(-10)); // -> true
console.log(Boolean(" ")); // -> true
console.log(Boolean("qwerty")); // -> trueconsole.log(5 * null);