JavaScript is a 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 the ‘+’ 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.
Example: This code shows the implicit type conversion in JavaScript.
javascript
console.log( '("3" - "1") = ' + ( "3" - "1" )); console.log( '("3" - 1) = ' + ( "3" - 1)); console.log( '("3" * "2") = ' + ( "3" * "2" )); console.log( '("3" % "2") = ' + ( "3" % "2" )); console.log( '("3" + null) = ' + ( "3" + null )); |
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: let v = 1555; let s = String(v); Output: now s contains "1555".
Syntax of toString() function:
variableName.toString(base)
Example:
Input: let v = 1555; let s = v.toString(); Output: now s contains "1555".
Example 2: Below code going to convert the number to string, boolean value to string and dates to string.
javascript
// Number and date has been assigned // to variable v and d respectively let v = 123; let d = new Date( '1995-12-17T03:24:00' ); // Conversion of number to string console.log( " String(v) = " + String(v)); // Conversion of number to string console.log( " String(v + 11) = " + String(v + 11)); console.log( " String( 10 + 10) = " + String(10 + 10)); // Conversion of boolean value to string console.log( " String(false) = " + String( false )); // Conversion of Date to string console.log( " String(d) = " + String(d)); |
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 the case of strings of non-numbers, it will convert it to a NaN(Not a Number)
Syntax:
Number(valueToConvert)
Example:
Input: let s = "144"; let n = Number(s); Output: now n contain 144(Number).
Example 3: Below code converts a numerical text, dates and boolean values to a number.
javascript
// Number and date has been assigned // to variable v and d respectively let v = "144" ; let d = new Date( '1995-12-17T03:24:00' ); // Conversion of string to number console.log( " Number(v) = " + Number(v)); //Conversion of boolean value to number console.log( " Number(false) = " + Number( false )); console.log( " Number(true) = " + Number( true )); // Conversion of date to number console.log( " Number(d) = " + Number(d)); |
Output:
Number(v) = 144 Number(false) = 0 Number(true) = 1 Number(d) = 819150840000
Example 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.
javascript
// Empty string assigned let v = "" ; // White space assigned let d = " " ; // Non-number string assigned let s = "neveropen" ; // Printing converted values of number console.log( " Number(v) = " + Number(v)); console.log( " Number(d) = " + Number(d)); console.log( " Number(s) = " + Number(s)); |
Output:
Number(v) = 0 Number(d) = 0 Number(s) = NaN