JavaScript tutorial-data type conversion


JavaScript data type conversion

In JavaScript , you do not need to specify the data type of a variable when you declare it. It means that the data type of variable takes form when the variable is assigned a value. To convert it to different type, you need only assign different type of value to it.

Example:

var result = 10;

This variable is numerical variable because the numerical value 42 was assigned to it. To convert to textural variable or string variable, you can assign a string value to it.

Example:

var result = "Have a nice weekend!";

Note: In expressions involving numerical and string values, JavaScript converts the numerical values to strings by using + sign.

Example:

var x = "The result is: " + 10;
var y = 10 + " is the result.";

If you want to convert a string that contain numerical characters to integer type and float type you can use parseInt() and pareFloat() respectively.

Example:

var i="10";

var in=parseInt(i);

var fl=parseFloat(i);

 





Comments




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.