1.5
Type conversion and coercion
Examples with a Run button are editable. Underlined terms open a short definition.
Type Conversion and Coercion
- Type Conversion Changing type of the Data explicitly.
- Type Coercion Javascript changing type of the Data implicitly.
Type Conversion
const inputYear = '1991';
// number() for String to Number
console.log(Number(inputYear), inputYear); // 1991 '1991'
console.log(Number(inputYear) + 18); // 2009
console.log(Number('ChandraPrakash')); // NaN
// String() for Number to String
console.log(String(23)); // 23
Falsy values
[ 0 , "" , undefined , null , NaN , false ] are false if they are converted to Boolean
console.log(Boolean(0)); // false
console.log(Boolean('')); // false
console.log(Boolean(NaN)); // false
console.log(Boolean(false)); // false
console.log(Boolean(undefined)); // false
console.log(Boolean(null)); // false
Truely Value
All values are truely if it is not [ 0 , "" , undefined , null , NaN , false ]
Type Coercion
// 23 Automatically converted to String while logging the String.
// " + " convert number to String
console.log('I am ' + 23 + ' years old.'); // I am 23 years old.
// " - " , " / " , " * " and also logical operators
// convert String to Number
console.log('23' - '10' - 3); // 10
console.log('23' > '10'); // true
// Example
let n = '1' + 1; // 11
n = n - 1; // 10
console.log(n);Your last opened chapter is saved in this browser when local storage is available.
Discuss this section
Questions, corrections or a better example? Comments use GitHub Discussions.