Javascript programming (course 4)

Source: Internet
Author: User

Someone told me in the previous section that I couldn't understand the second lesson. I don't know if it was too general or not meticulous, or something else. What's the problem, I don't know either.

What is a js guru, just want to bring fans into the door. We hope you will participate in this process more.
Today's main task is the for loop. The other is the data type. For in (array and object are not mentioned yet. First, let's take a look at it ).

Data type conversion:
If the data type of the operation is not the same, the js script will try its best to execute internal conversions, but js does not understand your mind. The result may be different from what you want.
Em:
3 + 3 // result = 6
3 + "3" // result = "33"

Convert a string to a numeric value:
The javascript language provides two built-in functions to convert a string that represents a value to a real value: parseInt () and parseFloat ().
To use these functions, you must pass the converted string as a parameter to the function. For example:
ParseInt ("42") // result = 42
ParseInt ("42.33") // result = 42
The value returned by the function is an integer or a floating point. If there is no rounding, the decimal point and the number following it will be discarded.
ParseFloat () returns a floating point number (if it is an integer, it returns an integer), for example:
ParseFloat ("42") // result = 42
ParseFloat ("42.33") // result = 42.33
If you want to convert a string somewhere, you just need to insert the function to the beginning. For example:
3 + 3 + parseInt ("3") // result = 9

Convert a value to a string:
Although JavaScript tends to be a string when expressions contain mixed data types. But in order to prevent potential problems, it is best to first switch the following. Add an empty string to the value.

Convert the value to a string:
("& Quot; + 2500) // result =" 2500"
("& Quot; + 2500). length // result = 4


For:
The most common loop structure in javascript is called a for loop. The keywords are placed at the beginning of the loop structure. The formal syntax structure is as follows:
For ([initial expression]; [condition]; [update expression]) {
Statement [s] inside loop
}
Example:
For (var I = 0; I <9; I ++)
{
N + = I
Myfunc (n)
}
For... in:
This statement determines the number of running times based on 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
}
Example:
<Script language = "javascript">
Document. writeln ("The properties of the document object ")
For (var element in document ){
Document. writein (element + "=" + document [element])
}
</Script>


Job:
Use the for loop to obtain several data records and display them 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.