Type Conversion in JavaScript
In the Primitive Values and Object References blog, we have learned about Primitive Values and Object References. Primitive values and object references in JavaScript refer to the two main types of data that the language uses. Primitives include basic data types like undefined, null, booleans, numbers, andstrings and are immutable (values cannot be changed). Objects encompass more complex structures, like arrays and functions, and are mutable (values can be modified). In this blog, we will learn about Type Conversion in JavaScript.
Type Conversion
In simple words, Type Conversion is nothing but the conversion of a value from one datatype to another. In JavaScript, we can change the datatype of any variable from one to another. For example, we can convert string to number, number to string, string to boolean, boolean to string, etc. Let’s take a look at the example:
Below is the Type Conversion Table:
Value | To String | To Number | To Boolean |
---|---|---|---|
undefined | “undefined” | NAN | FALSE |
null | “null” | 0 | FALSE |
true | “true” | 1 | TRUE |
false | “false” | 0 | FALSE |
“” (empty String) | “” | 0 | FALSE |
“1.2” | “1.2” | 1.2 | TRUE |
“one” | “one” | NAN | TRUE |
0 | “0” | 0 | FALSE |
1 | “1” | 1 | TRUE |
infinity | “infinity” | infinity | TRUE |
-infinity | “-infinity” | -infinity | TRUE |
NAN | “NAN” | NAN | FALSE |
(any object) | [object Object] | NAN | TRUE |
[](empty array) | ‘’ | 0 | TRUE |
[9](one numeric element) | “9” | 9 | TRUE |
[‘a’](any other array) | “a” | NAN | TRUE |
function()(any function) | NAN | TRUE |
Types of Conversion
There are two types of Type Conversion:
- Implicit Type Conversion
- Explicit Type Conversion
Implicit Type Conversion
If a compiler converts one data into another type of data automatically is called Implicit Type Conversion. The advantage of this type of conversion is there is no data loss. Let’s take a look at the example:
Explicit Type Conversion
When data of one type is converted explicitly into another type with the help of a predefined function, it is called Explicit Type Conversion. There is a data loss because the conversion is done forcefully. Let’s take a look at an example:
Conversion and Equality
JavaScript's == equality operator is quite flexible due to its ability to convert values. For example, it considers null andundefined as equal. It also treats certain strings, numbers, andbooleans as equivalent in certain cases. However, JavaScript also has a strict equality operator === which doesn't perform these conversions. It's essential to note that just because a value can be converted to another type doesn't mean they are equal. For example, while undefined converts to false in certain contexts, undefined is not equal to false. JavaScript's operators and statements handle values of different types and may perform conversions, but the ==operator doesn't convert its operands to booleans. Let's take a look at the example
Type conversion in JavaScript is a fundamental aspect of the language that allows for flexible data manipulation. Through implicit and explicit conversions, JavaScript enables developers to seamlessly work with different data types and perform various operations.