JavaScript Programming Start (Fourth lesson) _ Basics

Source: Internet
Author: User
Tags numeric value
Some people told me that the second lesson is not clear, I do not know whether the writing is too general, not meticulous, or other, what is the problem, everyone to put forward, of course, I do not

is what JS master, just want to bring the fans in the door just. I hope everyone will participate.
The main task today is for loops. The other is the data type. For-in (now not talking about arrays and objects, understand first).

Conversion of data types:
If the data type of the operation is not the same, JS script will try to perform internal conversion to solve, but JS does not understand your mind. So the result may be different from what you want.
Em:
3+3//Result=6
3+ "3"//result= "33"

To convert a string to a numeric value:
The JavaScript language provides two built-in functions to convert strings representing numeric values to real values: parseint () and parsefloat ().
In order to use these functions, you need to pass the converted string as a parameter to the function, for example:
parseint ("a")//result=42
parseint ("42.33")//result=42
is a floating-point number or an integer, the value returned by the function is an integer. There is no rounding, the decimal point and the number behind it will be discarded.
Parsefloat () returns a floating-point number (an integer if it is an integer), for example:
Parsefloat ("a")//result=42
Parsefloat ("42.33")//result=42.33
If you need to convert a string somewhere, simply insert the function at the beginning. Such as:
3+3+parseint ("3")//result=9

Converts a numeric value to a string:
Although the expression contains mixed data types, JS tends to be a string. However, to prevent potential problems, it is best to convert the following first. Adding an empty string to a number can

Converts a value to a string:
("" +2500)//result= "2500"
("+2500"). Length//result=4

For
The most commonly used loop structure in JavaScript is called A For loop, where the keyword is placed at the beginning of the loop structure. The formal grammatical structure is as follows:
for ([Initial expression];[ Condition]; [Update expression]) {
Statement[s] Inside Loop
}
Cases:
for (Var i=0;i<9;i++)
{
N+=i
MyFunc (N)
}
For...in:
This statement determines the number of times to run exactly according to the value set by the variable var. You can use the For...in statement to create a loop on an object or an array
for (var in [obj array])
{
Statements
}
Cases:
<script language= "JavaScript" >
Document.writeln ("The properties of the Document Object")
for (var element in document) {
Document.writein (element+ "=" +document[element])
}
</script>

Homework:
Use a For loop to get a few data that is displayed on the page. Familiarize yourself with the For loop.
(For loop is the most important one in control)
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.