Introduction
There are two types of type conversions (or) typecasting,
- Implicit Conversion (casting) done by JavaScript engine
- Explicit Conversion (casting) is done by the developer (or) programmer
Implicit type Conversions
Converting one type of data value to another type is called typecasting. If required JavaScript engine automatically converts one type of value into another type. It will try to convert it to the right type. We can see the example below:
E.g.
- <!DOCTYPE html>
- <html>
- <head>
- <title>Implicit Type Conversion</title>
- </head>
- <body>
- <h2>Implicit Type Conversion</h2>
- <script type="text/javascript">
- document.write (4+5.5);
- document.write("<br>");
- document.write("The type is :"+typeof(4+4.5));
- </script>
- </body>
- </html>
Output
In the above example, with four + five point three, we get the output nine point five. How the browser evaluates this expression, browser checks what operator you give to this expression. In this expression giving (‘+’) operator on the left side of the operator is an integer value and the right side of the operator real numbers. First, the browser converts the integer number to real number by putting (4.0+5.5) the add these values gives the output 9.5. Implicitly the browser converts integer numbers before evaluating the expression. We can see another example below:
Example
Output
45
In this example, the browser evaluates the expression to check the type of value that you're passing to the operator in the left side number type of value right side we have a string type of value. The browser implicitly converts the number to a string (“4” +”5”) two string the (+) operator acts as a concatenation operator, then it evaluates the expression string the final output is string value 45. Here you see the type of the expression using the keyword “type of”, it is automatically done by the JavaScript engine.
Example
- document.write (type of (4+”5”));
Let’s see another example for implicit type conversion:
Example
- <!DOCTYPE html>
- <html>
- <head>
- <title>Implicit Type Conversions </title>
- </head>
- <body>
- <h2>Implicit Type Conversions </h2>
- <script type="text/javascript">
- document.write(10+"5");
- document.write("<br>");
- document.write("The type is:"+typeof(10+"5"));
- document.write("<br>");
- document.write(10*"5");
- document.write("<br>");
- document.write("The type is :"+typeof(10*"5"));
- document.write("<br>");
- document.write(10+9.6);
- document.write("<br>");
- document.write("The type is:"+typeof(10+9.6));
- document.write("<br>");
- document.write(10*"hello");
- document.write("<br>");
- document.write("The type is:"+typeof(10*"hello"));
- </script>
- </body>
- </html>
Output
Explicit Type Conversions
In explicit conversion to convert one type of value into another type. explicit type conversion is done by programmers using JavaScript functions. The JavaScript programmer can explicitly convert the data type. we can see the function using an explicit type conversion
- Number () – it converts a number to string and Boolean to string
- Boolean () – converts any type of given value into true or false (or) 0 and 1
- ParseInt () – the given value to converts an integer
- ParseFloat () – the given value to converts a float
- String () – it converts any type of given value into string type
- toString () – it converts the given number into the binary, octal, and hexadecimal
Let's see the functionalities of the functions:
Number ()
It will change the string type or any type of value converts into a number type. we can see the example to convert the string to a number.
Example
- <!DOCTYPE html>
- <html>
- <head>
- <title>Number Type Conversion</title>
- </head>
- <body>
- <h2>Number Type Conversion</h2>
- <script type="text/javascript">
- var num1 = '30';
- document.write(Number(num1));
- document.write("<br>");
- document.write("The type of num1 is :"+typeof(Number(num1)));
- document.write("<br>");
- </script>
- </body>
- </html>
Output
Boolean ()
It converts a given value to a true or false (or) zero’s and one’s. if you give any value within the curly braces (“hello”) true or () false.
Example
- <!DOCTYPE html>
- <html>
- <head>
- <title>Boolean Type Conversion</title>
- </head>
- <body>
- <h2>Boolean Type Conversion</h2>
- <script type="text/javascript">
- var num1 = '30';
- document.write(Boolean("hello"));
- document.write("<br>");
- document.write("The type is :"+typeof(Boolean("hello")));
- document.write("<br>");
-
- document.write(Boolean(""));
- document.write("<br>");
- document.write("The type is :"+typeof(Boolean("")));
- document.write("<br>");
- </script>
- </body>
- </html>
Output
ParseInt ()
It converts a given value to an integer number, if not possible to convert the return NaN value.
In the below example, if the given value is of integer type then it returns as an integer.
E.g.
- document.write (24);
- document.write (12+12);
If the given value is the float type, then it discards float part and returns integer part of the number.
- document.write (parseInt (24));
- document.write (parseInt (12+12.4);
If the given value is of string type, it tries to extract and return the beginning integer part. If a string passed to the parseInt function doesn’t begin with an integer then it returns NaN value.
- document.write (parseInt (24));
- document.write (parseInt (12.4);
- document.write (parseInt (12 “hello world”);
Example
- <!DOCTYPE html>
- <html>
- <head>
- <title></title>
- </head>
- <body>
- <h2>ParseInt Type conversion</h2>
- <script type="text/javascript">
- document.write ("THe integer type is :"+parseInt (24.89));
- document.write("<br>");
- </script>
- </body>
- </html>
Output
ParseFloat ()
The ParseFloat () function returns a floating-point number that parses a string. This function determines whether the first character of a particular string is a number.
Example
- <!DOCTYPE html>
- <html>
- <head>
- <title></title>
- </head>
- <body>
- <h2>ParseInt Type conversion</h2>
- <script type="text/javascript">
- document.write ("The float type is :"+parseFloat(24.89));
- document.write("<br>");
- document.write ("The float type is :"+parseFloat(24));
- document.write("<br>");
- document.write("The float type is"+typeof(parseFloat(24)));
- document.write("<br>");
- </script>
- </body>
- </html>
Output
String ()
It converts any type of given value into a string type. Using key string () explicitly converts the string value.
Example
- <!DOCTYPE html>
- <html>
- <head>
- <title>String Type Conversion</title>
- </head>
- <body>
- <h2>String Type Conversion</h2>
- <script type="text/javascript">
- var num1 = '30 in a number';
- document.write(String(num1));
- document.write("<br>");
- document.write("The type of num1 is :"+typeof(String(num1)));
- document.write("<br>");
- </script>
- </body>
- </html>
Output
tostring ()
It is used with a number and converts the number into a string. It is used to return a string function. it converts the given number into the binary, octal, and hexadecimal.
Example
- <!DOCTYPE html>
- <html>
- <head>
- <title>tostring Type Conversion</title>
- </head>
- <body>
- <h2>tostring Type</h2>
- <script type="text/javascript">
- var num1 = '30';
- var num2 = 30;
- var x;
- x=num2.toString(2);
- document.write(x);
- document.write("<br>");
- document.write("The type of x is :"+typeof(x));
- document.write("<br>");
- x=num2.toString(8);
- document.write(x);
- document.write("<br>");
- document.write("The type of x is :"+typeof(x));
- document.write("<br>");
- </script>
- </body>
- </html>
Output
Summary
In this article, we have learned about Type conversions in JavaScript. I hope this article is useful to you. Thanks for reading!