Java Script Data Type Conversion | Integer to String | String to Integer

In JavaScript Datatype conversion is basically to conversion of datatype from string to an integer, integer to a string, string to floating point number. Below are the ways that how to convert the datatype in JavaScript. It is very simple topic but an important topic.

JavaScript Datatype Conversion with Examples:

To determine the datatype of any variable

gs.print(typeof variablename)

Convert an Integer value to String

variablename.toString();

Conversion of String to and Integer

parseInt(variablename);

** Note: if it is a number and it is in string (for e.g. “10”) then only it will be converted to number, otherwise the output would be NaN (Not a number)

Convert a String to float

 parseFloat(variablename);

Few e.g., are given below:

 // Conversion of Integer to String

var x=15;

var y=x.toString();

gs.print(typeof x);

// Conversion of String to Integer

var stringValue="JavaScript";

var numberValue=parseInt(stringValue);

gs.print(typeof numberValue);

gs.print(numberValue);


e.g. of the use of Data Type Conversion:

var x=15;     // her x contains the number value

var y="15";  // here y contains is string value

// String equality comparision operator

if(x==y){

gs.print("Hey Both the values are same");

}

else{

gs.print("the Type of both the values are not same");

}


another example:

var x=15;     // her x contains the number value

var y="15";  // here y contains is string value

// Strict String equality comparision operator which also compare the typeof variable-- this is the suggested way to perform

if(x===y){

gs.print("Hey Both the values are same");

}

else{

gs.print("the Type of both the values are not same");

}

Above are some examples of JavaScript datatype conversion. If you have any questions related to this topic, please email us or comment the same below.

Post a Comment

0 Comments