In this lesson, we will learn different types of Javascript Type Conversions that are performed in JavaScript. Type conversion in JavaScript is an algorithm that allows us to manipulate the function’s data types from one to another. Also, the Javascript compiler often performs these conversions and ensures that the function gets the correct inputs depending on the operator used.
There are two types of type conversion in JavaScript.
- Implicit Conversion
- Explicit Conversion
Implicit Conversion
A Javascript implicit conversion is when a data type is converted into another so that the operators or functions will work correctly and are automatically performed. These conversions are also known as type coercion in Javascript. There are different types of implicit conversion available in javascript.
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"
The +
operator converts a number into a string and concatenates the values.
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
In implicit conversion to number, a string number is first converted into a number, then -
, /
and *
operations are performed.
Example 3: Non-numeric String Results to NaN
let result;
result = 'string' - 'next';
console.log(result) // NaN
result = '4' - 'minus';
console.log(result) // NaN
Here, when -
, /
and * operations are performed on two or more non-numeric strings the result provided is NaN
.
Example 4: Implicit Boolean Conversion to Number
let result;
result = '4' - true;
console.log(result); // 3
result = 4 + true;
console.log(result); // 5
result = 4 + false;
console.log(result); // 4
JavaScript considers 0 as false
and all non-zero numbers as true
. And, if true
is converted to a number, the result is always 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
Here, the null
the keyword represents 0 (zero) when it is used with the number in any operation.
Explicit Conversion
Explicit conversion is the type of conversion where a user converts one data type to another data type depending on their scenarios. There are different built-in methods available in javascript to perform the explicit conversion. Some examples are listed below:
Example 1: Convert to Number Explicitly
We can use theNumber
method to convert the numeric strings and boolean values to numbers.
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
result = Number(null);
console.log(result); // 0
let result = Number(' ')
console.log(result); // 0
Example 2: Convert to String Explicitly
We can use either String()
or toString()
method to convert the other data types to strings.
//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"
Example 2: Convert to Boolean Explicitly
We can use Boolean()
method to convert the other data types to boolean. In JavaScript, undefined
, null
, 0
, NaN
, ''
converts to false
. And other values return true
.
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
result = Boolean(324);
console.log(result); // true
result = Boolean('hello');
console.log(result); // true
result = Boolean(' ');
console.log(result); // true
Conclusion
In this lesson, you have learned about different types of javascript type conversion and how they are performed.