Javascript - Data Types and Variables - Javascript parse int
Ocak 12, 2011 by Javascript
|
|
Article Description
This topic contains and tries to cover following subjects:
- Brief explanation of data conversion in Javascript
- Explanation of Javascript parse int
- Example to converting string to number in Javascript
Articles tries to provide answer to following questions and issues:
- How to convert number to string in Javascript
Articles pre-requisites following information:
- General knowledge of variables in Javascript
- General knowledge of functions in Javascript
- General knowledge of data concatenation in Javascript
Brief explanation of Javascript Variables
In this javascript tutorial, we will cover data conversion and converting string s to number in javascript. In javascript, to convert a string to number parseInt() method is used. parseInt() is a function which accepts string as parameter, and parses string char by char to convert it into a number. Finally, parseInt returns converted number.
Lets look into the syntax:
var v_planetName = "95saturn";
var v_number = parseInt(v_planetName);
document.write(v_number);
In above javascript code block, we created a string variable which contains both number and string together as a string . We created another variable to store number of parseInt() result. Finally, we displayed it with write function.
If you check code in browser, result says "95". This demonstrates how parseInt handles conversion. When function start to parse, it converts char by char the parameter provided to it till it encounters a char which is not number. When function encounters a char which is not number, it stops parsing and returns the processed char s it parsed. Our example had two char which were number, therefore, function returned "95". Javascript does not cause compiler error in such case, it parses till it encounters a char which is not number.
Brief explanation of Javascript Variables
Following example demonstrates javascript parse int in example. In our html page, we will declare a javascript variable string containing number and string inside. We will parse it into number from string .
In above javascript sting to number conversion example, javascript parsed, browser indicates that parseint function converted our string to number as 95.
Data Layers
Area: | programming \ languages \ javascript \ \ \ |
Ref: | |
Loc: | articles |
Tags: | javascript |
|