In this tutorial, you will learn about type conversions in JavaScript with the help of examples.
More often than not, operators and functions consequently convert the qualities given to them to the right sort.
For instance, an alert automatically changes any incentive over to a string to show it. Mathematical activities convert values to numbers.
There are likewise situations when we need to expressly change an incentive over to the normal sort.
The process of converting one data type to another data type is called type conversion. There are two sorts of type conversions in JavaScript.
- Implicit Conversion
- Explicit Conversion
In this article, you will learn-
- 1 JavaScript Implicit Conversion
- 2 Example 1: Implicit Conversion to String
- 3 Example 2: Implicit Conversion to Number
- 4 Example 3: Non-numeric String Results to NaN
- 5 Example 4: Implicit Boolean Conversion to Number
- 6 Example 5: null Conversion to Number
- 7 Example 6: undefined used with a number, boolean or null
- 8 JavaScript Explicit Conversion
- 9 2. Convert to String Explicitly
- 10 3. Convert to Boolean Explicitly
- 11 JavaScript Type Conversion Table
JavaScript Implicit Conversion
In specific circumstances, JavaScript consequently changes over one data type to another (to the right type). This is known as implicit conversion.
Example 1: Implicit Conversion to String
// numeric string used with + gives string type
let result;
result = '3' + 2;
console.log(result) // "32"
result = '3' + true;
console.log(result); // "3true"
result = '3' + undefined;
console.log(result); // "3undefined"
result = '3' + null;
console.log(result); // "3null"
Note: When a number is added to a string, JavaScript changes the number over to a string before connection.
Example 2: Implicit Conversion to Number
// numeric string used with - , / , * results number type
let result;
result = '4' - '2';
console.log(result); // 2
result = '4' - 2;
console.log(result); // 2
result = '4' * 2;
console.log(result); // 8
result = '4' / 2;
console.log(result); // 2
Example 3: Non-numeric String Results to NaN
// non-numeric string used with +, - , / , * results to NaN
let result;
result = 'hello' - 'world';
console.log(result); // NaN
result = '4' - 'hello';
console.log(result); // NaN
Example 4: Implicit Boolean Conversion to Number
// if boolean is used, true is 1, false is 0
let result;
result = '4' - true;
console.log(result); // 3
result = 4 + true;
console.log(result); // 5
result = 4 + false;
console.log(result); // 4
Note: JavaScript considers 0 as false and all non-zero numbers as true. Also, if true is changed over to a number, the result is consistently 1.
Example 5: null Conversion to Number
// null is 0 when used with number
let result;
result = 4 + null;
console.log(result); // 4
result = 4 - null;
console.log(result); // 4
Example 6: undefined used with a number, boolean or null
// Arithmetic operation of undefined with number, boolean or null gives NaN
let result;
result = 4 + undefined;
console.log(result); // NaN
result = 4 - undefined;
console.log(result); // NaN
result = true + undefined;
console.log(result); // NaN
result = null + undefined;
console.log(result); // NaN
JavaScript Explicit Conversion
You can likewise convert one data type to another according to your necessities. The sort of change that you do manually is known as explicit type conversion.
In JavaScript, explicit sort conversions are finished using built-in strategies.
Here are some basic strategies of explicit conversions.
1. Convert to Number Explicitly
To change numeric strings and boolean qualities over to numbers, you can use Number(). For instance,
let result;
// string to number
result = Number('324');
console.log(result); // 324
result = Number('324e-1')
console.log(result); // 32.4
// boolean to number
result = Number(true);
console.log(result); // 1
result = Number(false);
console.log(result); // 0
In JavaScript, empty strings and null values return 0. For example,
let result;
result = Number(null);
console.log(result); // 0
let result = Number(' ')
console.log(result); // 0
If a string is an invalid number, the result will be NaN. For example,
let result;
result = Number('hello');
console.log(result); // NaN
result = Number(undefined);
console.log(result); // NaN
result = Number(NaN);
console.log(result); // NaN
Note: You can likewise create numbers from strings using parseInt(), parseFloat(), unary operator + and Math.floor(). For instance,
let result;
result = parseInt('20.01');
console.log(result); // 20
result = parseFloat('20.01');
console.log(result); // 20.01
result = +'20.01';
console.log(result); // 20.01
result = Math.floor('20.01');
console.log(result); // 20
2. Convert to String Explicitly
To change over other data types to strings, you can use either String() or toString(). For instance,
//number to string
let result;
result = String(324);
console.log(result); // "324"
result = String(2 + 4);
console.log(result); // "6"
//other data types to string
result = String(null);
console.log(result); // "null"
result = String(undefined);
console.log(result); // "undefined"
result = String(NaN);
console.log(result); // "NaN"
result = String(true);
console.log(result); // "true"
result = String(false);
console.log(result); // "false"
// using toString()
result = (324).toString();
console.log(result); // "324"
result = true.toString();
console.log(result); // "true"
Note: String() takes null and undefined and converts them to string. However, toString() gives an error when null is passed.
3. Convert to Boolean Explicitly
To convert other data types to a boolean, you can use Boolean().
In JavaScript, undefined, null, 0, NaN, ” converts to false. For example,
let result;
result = Boolean('');
console.log(result); // false
result = Boolean(0);
console.log(result); // false
result = Boolean(undefined);
console.log(result); // false
result = Boolean(null);
console.log(result); // false
result = Boolean(NaN);
console.log(result); // false
All other values give true. For example,
result = Boolean(324);
console.log(result); // true
result = Boolean('hello');
console.log(result); // true
result = Boolean(' ');
console.log(result); // true
JavaScript Type Conversion Table
The table shows the conversion of various qualities to String, Number, and Boolean in JavaScript.
Value | String Conversion | Number Conversion | Boolean Conversion |
1 | “1” | 1 | true |
0 | “0” | 0 | false |
“1” | “1” | 1 | true |
“0” | “0” | 0 | true |
“ten” | “ten” | NaN | true |
true | “true” | 1 | true |
false | “false” | 0 | false |
null | “null” | 0 | false |
undefined | “undefined” | NaN | false |
” | “” | 0 | false |
‘ ‘ | ” “ | 0 | true |
You will learn about the conversion of objects and arrays to other data types in later tutorials.
Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.