Table Of Contents
Type conversion can be explicit or implicit.
value = Number('23') // explicit
value = 5 + '25' // implicit
Value type checking
console.log(typeof value);
String conversion
Number to string:
value = String(10); /* => '10' */
value = String(10 + 40); /* => '50' */
value = (10 + 40).toString(); /* => '50' */
value = new String(10 + 20); /* => '30' */
Boolean to string
value = String(true); /* => 'true' */
value = String(false); /* => 'false' */
Array to string
value = String([1, 2, 3]); /* => '1,2,3' */
value = String([ ]); /* => '' */
Object to string
value = String({name: "Daniel"}); /* => [object Object] */
Conversion to string occurs when any data type is concatenated with a string (implicit conversion):
value = 30 + ' ' + 30; /* => 30 30 */ // Space is considered a symbol.
value = 30 + '' + undefined; /* => 30undefined */
Mathematical operations convert Empty String to zero:
value = 30 - ''; /* => 30 */
value = 30 - 'text'; /* => NaN */ // If the string is not empty, then we will get NaN - calculation error.
value = 30 - '5'; /* => 25 */ // If we write a number in a string, we will get a number type
Boolean type conversion
In mathematical operations, true is converted to one and false to zero:
value = true + 5; /* => 6 */
value = false + 5; /* => 5 */
String to boolean
value = Boolean('hello'); /* => true */ // Any non-empty string will be considered true.
value = Boolean(' '); /* => true */
value = Boolean(''); /* => false */ // An empty string will be considered false.
Number to boolean
value = Boolean(-123); /* => true */ // Any number, both positive and negative, will be considered true.
value = Boolean(123); /* => true */
value = Boolean(0); /* => false */ // Zero counts as false
Undefined to boolean
value = Boolean(undefined); /* => false */
Null to boolean
value = Boolean(null); /* => false */
Object to boolean
value = Boolean({}); /* => true */ // An empty object is considered true.
Array to boolean
value = Boolean([]); /* => true */ // An empty array is considered true.
Number type conversion
String to number
value = Number('23'); /* => 23 */
value = Number('string...lalala'); /* => NaN */
value = parseInt(' 203px'); /* => 203 */ // The parseInt function reads a number from a string and removes all characters after it, but if there are characters before the number (except for a space), then it will output NaN. Serves for whole numbers.
value = parseFloat('203.212px'); /* => 203.212 */ // Works the same as parseInt, but for fractional numbers.
Boolean to number
value = Number(true); /* => 1 */
value = Number(false); /* => 0 */
Null to number
value = Number(null); /* => 0 */
Array to number
value = Number([1, 2, 3]); /* => NaN */ // NaN refers to numbers.
Null type conversion
Converts to zero for mathematical operations:
value = null + 5; /* => 5 */
Undefined type conversion
Converts to NaN for mathematical operations:
value = false + undefined; /* => NaN */
Thank's for reading! β€οΈ
You can also check out my redux basics cheat sheet
Top comments (6)
Nice !
Glad you like it!
Excellent π
Thanks! Glad you liked it!
Good one!!
Thank you! π