JavaScript - type convertions

testYourselfJavaScript JavaScript61

We have few types in JavaScript like number, boolean, string, array, object, undefined, null, function, Date and symbol.

The type conversion in JavaScript takes place in two cases, the first without the developer's direct intervention when the operand or function doesn't receive expected parameter, and the second when the programmer does a type conversion purposely.

Let's analyze most of the possible type conversions in JavaScript:

String Conversion

String conversion happens when we need the string form of a value.

There are few general ways to convert any  value to the string: toString() method, String() function and string concatenation (if one of  the operands is a string, string concatenation is executed instead of adding).

var x;
console.log(x.toString());
console.log(String(x));
console.log("" + x);

number to string:

console.log(String(1)); // "1"
console.log(String(0)); // "0"
console.log(String(-1)); // "-1"

boolean to string:

console.log(String(true)); // "true"
console.log(String(false)); // "false"

array to string:

console.log(String([])); // ""
console.log(String([1])); // "1"
console.log(String([1,2,3])); // "1,2,3"

object to string:

var obj = {name: "John"};
console.log(obj.toString()); // "[object Object]"
console.log(JSON.stringify(obj)); // '{"name":"John"}'

other values:

console.log(String(null)); // "null"
console.log(String(undefined)); // "undefined"
console.log(String(NaN)); // "NaN"

All values:

value string value
null "null"
undefined "undefined"
NaN "NaN"
1 "1"
0 "0"
-1 "-1"
true "true"
false "false"
[] ""
[1,2,3] "1,2,3"

Number conversion

Number convertion happens when we need the number form of a value.

There are few general ways to convert any value to the number: Number() function, parseInt() function, parseFloat() function and addition operator.

string to integer

var b = "5";
console.log(Number(b)); // 5
console.log(parseInt(b)); // 5
console.log(+b); // 5

Whitespaces from the start and end are removed. If the remaining string is empty, the result is 0.

console.log(Number("   10   ")); // 10
console.log(Number("3.14")); // 3.14
console.log(Number("  ")); // 0
console.log(Number(" ")); // 0
console.log(Number("")); // 0

boolean to integer:

var bool = true;
console.log(Number(bool)); // 1
console.log(parseInt(bool)); // NaN
console.log(+bool); // 1

boolean to integer:

console.log(Number(true)); // 1
console.log(Number(false)); // 0

others values:

console.log(Number(undefined)); // NaN
console.log(Number(null)); // 0

If the value is not a valid number, the result of such conversion is NaN.

console.log(Number("10a")); // NaN
console.log(Number("xyz")); // NaN
console.log(Number(NaN)); // NaN

All values

value number value
null 0
undefined NaN
NaN NaN
"1" 1
"0" 0
"3.14" 3.14
"" 0
" " 0
"a" NaN
true 1
false 0

Boolean Conversion

Boolean convertion happens when we need the boolean form of a value.

There are few general ways to convert any value to boolean value: Boolean() function, double negation (!!)

number to boolean:

console.log(Boolean(1)); // true
console.log(Boolean(0)); // false
console.log(Boolean(-1)); // true

string to boolean:

console.log(Boolean("")); // false
console.log(Boolean("a")); // true
console.log(Boolean(" ")); // true
console.log(Boolean("0")); // true

other values:

console.log(Boolean(undefined)); // false
console.log(Boolean(null)); // false
console.log(Boolean(NaN)); // false
console.log(Boolean([])); // true
console.log(Boolean({})); // true

All values

value boolean value
null false
undefined false
NaN false
1 true
0 false
-1 true
"" false
" " true
"a" true
"0" true
[] true
{} true