Loading Please Wait...

Logo Lynxsia IT Solutions

JavaScript Type Conversion

JS Type Conversion

JavaScript variables can be converted to a new variable and another data type.

Converting Strings to Numbers

The global method Number() converts a variables (or values, or expressions) into a number.

The method parseFloat() parses a string and returns a floating point number.

The method parseInt() parses a string and returns an integer.

The unary + operator() can be used to convert a variable to a number.

					 
        
          Number("3.14")  // 3.14
          Number(Math.PI) // 3.141592653589793
          Number(" ")     // 0
          Number("")      // 0
          Number("99 88") // NaN
          Number("John")  // NaN

          parseFloat("3.14")  // 3.14

          parseInt("3.14")  // 3

          let a = "5";      // a is a string
          let b = + b;      // b is a number

          let i = "John";   // i is a string
          let j = + i;      // j is a number (NaN)
        
      
Converting Numbers to Strings

The global method String() can convert numbers to strings. It can be used on any type of numbers, literals, variables, or expressions.

The method toString() returns a string.

The method toExponential() returns a string, with a number rounded and written using exponential notation.

The method toFixed() returns a string, with a number rounded and written with a specified number of decimals.

The method toPrecision() returns a string, with a number written with a specified length.

					 
        
          let a =123;

          String(a)              // "123"
          String(123)            // "123"
          String(100 + 23)       // "123"

          a.toString();          // "123"
          (123).toString();      // "123"
          (100 + 23).toString(); // "123"

          (123).toExponential(); // "1.23e+2"
          (123).toFixed();       // "123"
          (123).toPrecision();   // "123"
        
      
Converting Dates to Numbers

The global method Number() can be used to convert dates to numbers.

The date method getTime() returns a number from date.

					 
        
          d = new Date();
          Number(d);   // 1404568027739
          d.getTime(); // 1404568027739
        
      
Converting Dates to Strings

The global method String() can be used to convert dates to strings.

The method toString() returns a string from date.

					 
        
          String(Date());    // "Thu Jun 29 2023 20:15:04 GMT+0530 (India Standard Time)"
          Date().toString(); // "Thu Jun 29 2023 20:15:04 GMT+0530 (India Standard Time)"
        
      
Converting Booleans to Numbers

The global method Number() can also convert booleans to numbers.

					 
        
          Number(false);    // 0
          Number(true);     // 1
        
      
Converting Booleans to Strings

The global method String() can also convert booleans to strings.

The method toString() returns a string from booleans.

					 
        
          String(false); // "false"
          String(true) ; // "true"
          false.toString(); // "false"
          true.toString();  // "true"
        
      
Automatic Type Conversion

When JavaScript tries to operate on a "wrong" data type, it will try to convert the value to a "right" type. The result is not always what you expect.

					 
        
          5 + null    // 5         because null is converted to 0
          "5" + null  // "5null"   because null is converted to "null"
          "5" + 2     // "52"      because 2 is converted to "2"
          "5" - 2     // 3         because "5" is converted to 5
          "5" * "2"   // 10        because "5" and "2" are converted to 5 and 2
        
      

How you feel about this blog:

Share this blog on:

Report Us

If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com

Contact Us
Ads
Logo
Lynxsia IT Solutions

We are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..

Kotwali Road, Chhiptehri, Banda, 210001, UP, India

Copyright © 2022, Lynxsia IT Solutions, All rights reserved