Course
Type Conversions
JavaScript Tutorial
This JavaScript tutorial is crafted for beginners to introduce them to the basics and advanced concepts of JavaScript. By the end of this guide, you'll reach a proficiency level that sets the stage for further growth. Aimed at empowering you to progress towards becoming a world-class software developer, this tutorial paves the way for a successful career in web development and beyond.
Type Conversions
JavaScript Type Conversions
In JavaScript, type conversions refer to the process of converting a value from one data type to another. This is important because JavaScript is a dynamically typed language, meaning that variables can hold different types of values at different times.
There are two main types of type conversions in JavaScript: implicit and explicit.
Implicit Type Conversion
Implicit type conversion, also known as coercion, occurs when JavaScript automatically converts a value from one data type to another. This typically happens when performing operations between different data types.
For example:
Converting to String (Implicit conversion)
In this example, we used the '+' operator to implicitly convert different values to the string data type.
"100" + 24; // Converts 24 to string'100' + false; // Converts false boolean value to string"100" + null; // Converts null keyword to string
Please note that to convert a value to string using "+" operator, one operand should be string.
Let's try the example below, and check the output −
<html><head> <title>Implicit conversion to string </title></head><body> <script> document.write("100" + 24 + "<br/>"); document.write('100' + false + "<br/>"); document.write("100" + null+ "<br/>"); document.write("100" + undefined+ "<br/>"); </script></body></html>
Converting to Number (Implicit conversion)
When you use the string values containing the digits with the arithmetic operators except for the '+' operator, it converts operands to numbers automatically and performs the arithmetic operation, which you can see in the example below.
Boolean values are also gets converted to a number.
'100' / 50; // Converts '100' to 100'100' - '50'; // Converts '100' and '50' to 100 and 50'100' * true; // Converts true to 1'100' - false; // Converts false to 0'tp' / 50 // converts 'tp' to NaN
Try the example below and check the output
<html><head> <title> Implicit conversion to Number </title></head><body> <script> document.write(('100' / 50) + "<br>"); document.write(('100' - '50') + "<br>"); document.write(('100' * true) + "<br>"); document.write(('100' - false) + "<br>"); document.write(('tp' / 50) + "<br>"); </script></body></html>
Explicit Type Conversion
Explicit type conversion involves manually converting a value from one data type to another using built-in functions or operators. This allows you to control how values are converted between different types.
Some common methods for explicit type conversion include:
- Using the
Number()
function to convert a value to a number. - Using the
String()
function to convert a value to a string. - Using the
Boolean()
function to convert a value to a boolean.
Converting to String (Explicit conversion)
You can use the String() constructor to convert the numbers, boolean, or other data types into the string.
String(100); // number to stringString(null); // null to stringString(true); // boolean to string
Example
You can use the String() constructor function to convert a value to the string.You can also use typeof operator to check the type of the resultant value.
<html><head> <title> Converting to string explicitly </title></head><body> <script> document.write(typeof String(100) + "<br/>"); document.write(typeof String(null)+ "<br/>"); document.write(typeof String(true) + "<br/>"); </script></body></html>
We can also use the toString() method of Number object to convert number to string.
const num = 100;num.toString() // converts 100 to '100'
Converting to Number (Explicit conversion)
You can use the Numer() constructor to convert a string into a number. We can also use unary plus (+) operator to convert a string to number.
Number('100'); // Converts '100' to 100Number(false); // Converts false to 0Number(null); // Converts null to 0num = +"200"; // Using the unary operator
However, you can also use the below methods and variables to convert the string into numbers.
Example
You can use the Number() constructor function or unary (+) operator to convert a string, boolean, or any other value to a number.
The Number() function also converts the exponential notation of a number to a decimal number.
<html><head> <title> Converting to string explicitly </title></head><body> <script> document.write(Number("200") + "<br/>"); document.write(Number("1000e-2") + "<br/>"); document.write(Number(false) + "<br/>"); document.write(Number(null) + "<br/>"); document.write(Number(undefined) + "<br/>"); document.write(+"200" + "<br/>"); </script></body></html>
Converting to Boolean (Explicit conversion)
You can use the Boolean() constructor to convert the other data types into Boolean.
Boolean(100); // Converts number to boolean (true)Boolean(0); // 0 is falsy value (false)Boolean(""); // Empty string is falsy value (false)Boolean("Hi"); // Converts string to boolean (true)Boolean(null); // null is falsy value (false)
Example
You can use the Boolean() constructor to convert values to the Boolean. All false values like 0, empty string, null, undefined, etc., get converted to false and other values are converted to true.
<html><head> <title> Converting to string explicitly </title></head><body> <script> document.write(Boolean(100) + "<br/>"); document.write(Boolean(0) + "<br/>"); document.write(Boolean("") + "<br/>"); document.write(Boolean("Hi") + "<br/>"); document.write(Boolean(null) + "<br/>"); </script></body></html>