Loading course content...
Loading course content...
Code Playground is only enabled on larger screen sizes.
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.
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()); // -> ErrorThe Number() function can be used to convert other data types into numbers. The conversion obeys the following rules:
console.log(Number("\n\t ")); // -> 0console.log(Number("00001")); // -> 1
console.log(Number("12345")); // -> 12345
console.log(Number("-12345")); // -> -12345
console.log(Number("-0.001")); // -> -0.001console.log(Number("\t00001\t"));
console.log(Number("12345\n"));1
12345NaN.console.log(Number("000a01")); // -> NaN
console.log(Number("123 45")); // -> NaN
console.log(Number("-12-345")); // -> NaN
console.log(Number("-0.00.1")); // -> NaNtrue will become 1, and false will become 0.console.log(Number(true)); // -> 1
console.log(Number(false)); // -> 0null will become 0, and undefined will become NaN.console.log(Number(null)); // -> 0
console.log(Number(undefined)); // -> NaNThis is one of the few differences between null and undefined.
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);