JavaScript | Type Conversion
JavaScript is loosely typed language and most of the time operators automatically convert a value to the right type but there are also cases when we need to explicitly do type conversions.
While JavaScript provides numerous ways to convert data from one type to another but there are two most common data conversions :
- Converting Values to String
- Converting Values to Numbers
Implicit Conversion:
There are various operator and functions in JavaScript which automatically converts a value to the right type like alert() function in JavaScript accepts any value and convert it into a string. But various operator creates a problem like ‘+’ operator.
Example:
Input: "2" + "3" Output: "23" here + operator stands for string concatenation in this case. But "3" - "1" gives output 2 by using Implicit Conversion.
Code #1:
This code shows the implicit type conversion in JavaScript.
<script> document.write( '("3" - "1") = ' + ( "3" - "1" ) + "<br>" ); document.write( '("3" - 1) = ' + ( "3" - 1) + "<br>" ); document.write( '("3" * "2") = ' + ( "3" * "2" ) + "<br>" ); document.write( '("3" % "2") = ' + ( "3" % "2" ) + "<br>" ); document.write( '("3" + null) = ' + ( "3" + null ) + "<br>" ); </script> |
Output:
("3" - "1") = 2 ("3" - 1) = 2 ("3" * "2") = 6 ("3" % "2") = 1 ("3" + null) = 3null
Converting Values to Strings:
String() or toString() function can be used in JavaScript to convert a value to a string.
Syntax of String() function:
String(value)
Example:
Input: var v = 1555; var s = String(v); Output: now s contains "1555".
Syntax of toString() function:
variableName.toString(base)
Example:
Input: var v = 1555; var s = v.toString(); Output: now s contains "1555".
For more information on toString( ) function check this article JavaScript | toString( ) function.
Code #2:
Below code going to convert the number to string, boolean value to string and dates to string.
<script> // Number and date has been assigned // to variable v and d respectively var v = 123; var d = new Date( '1995-12-17T03:24:00' ); // Conversion of number to string document.write( " String(v) = " + String(v) + "<br>" ); // Conversion of number to string document.write( " String(v + 11) = " + String(v + 11) + "<br>" ); document.write( " String( 10 + 10) = " + String(10 + 10) + "<br>" ); // Conversion of boolean value to string document.write( " String(false) = " + String( false ) + "<br>" ); // Conversion of Date to string document.write( " String(d) = " + String(d) + "<br>" ); </script> |
Output:
String(v) = 123 String(v + 11) = 134 String( 10 + 10) = 20 String(false) = false String(d) = Sun Dec 17 1995 03:24:00 GMT+0530 (India Standard Time)
Converting Values to Numbers:
We can use Number() function in JavaScript to convert a value to a Number. It can convert any numerical text and boolean value to a Number. In case of strings of non-numbers it will convert it to a NaN(Not a Number).
Syntax:
Number(valueToConvert)
Example:
Input: var s = "144"; var n = Number(s); Output: now n contain 144(Number).
Code #3:
Below code converts a numerical text, dates and boolean values to a number.
<script> // Number and date has been assigned // to variable v and d respectively var v = "144" ; var d = new Date( '1995-12-17T03:24:00' ); // Conversion of string to number document.write( " Number(v) = " + Number(v) + "<br>" ); //Conversion of boolean value to number document.write( " Number(false) = " + Number( false ) + "<br>" ); document.write( " Number(true) = " + Number( true ) + "<br>" ); // Conversion of date to number document.write( " Number(d) = " + Number(d) + "<br>" ); </script> |
Output:
Number(v) = 144 Number(false) = 0 Number(true) = 1 Number(d) = 819150840000
code #4:
If the string is non-number then it converts it to NaN and strings of white spaces or empty strings will convert to 0.
<script> // Empty string assigned var v = "" ; // White space assigned var d = " " ; // Non-number string assigned var s = "GeeksforGeeks" ; // Printing converted values of number document.write( " Number(v) = " + Number(v) + "<br>" ); document.write( " Number(d) = " + Number(d) + "<br>" ); document.write( " Number(s) = " + Number(s) + "<br>" ); </script> |
Output:
Number(v) = 0 Number(d) = 0 Number(s) = NaN
Please Login to comment...