Home >Web Front-end >JS Tutorial >Getting Started with JavaScript Programming (Lesson 4)_Basic Knowledge
Someone told me some time ago that I couldn’t understand the second lesson. I don’t know if it was too general, not detailed enough, or something else. If you have any questions, please ask them. Of course, I can’t
What kind of js master is he? He just wants to bring fans in. Hope everyone can participate.
Today’s main task is the for loop. The other thing is the data type. Both for in (I haven’t talked about arrays and objects yet, let’s learn about them first).
Data type conversion:
If the data types of the operation are not the same, the js script will try its best to perform internal conversion to solve the problem, but js does not understand your thoughts. So the results you get may be different from what you want.
em:
3 3 // result=6
3 "3" // result="33"
Convert string to numeric value:
The JavaScript language provides two built-in functions to convert strings representing numerical values into real numerical values: parseInt() and parseFloat().
In order to use these functions, you need to pass the converted string into the function as a parameter, for example:
parseInt("42") //result=42
parseInt("42.33") //result=42
But whether it is a floating point number or an integer, the values returned by the function are all integers. There is no rounding, the decimal point and the digits following it are discarded.
ParseFloat() returns a floating point number (if it is an integer, return an integer), for example:
parseFloat("42") //result=42
parseFloat("42.33") //result=42.33
If you need to convert a string somewhere, just insert the function into the beginning. like:
3 3 parseInt("3") //result=9
Convert numeric value to string:
Although js will tend to string when encountering mixed data types in expressions. But in order to prevent potential problems from occurring, it is best to convert the following first. Just add an empty string to the value
Convert the value into a string:
("" 2500) //result="2500"
("" 2500).length //result=4
for:
The most commonly used loop structure in JavaScript is called a for loop, and 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
}
Example:
for(var i=0;i{
n =i
myfunc(n)
}
for...in:
This statement determines the number of runs based entirely on the value set in the variable var. You can create a loop over an object or an array using the for...in statement
for(var in [obj | array])
{
statements
}
Example:
Homework:
Use a for loop to get several data and display it on the page. Get familiar with the for loop.
<script>
<br>document.writeln("The properties of the document object")
<br>for(var element in document){
<br>document.writein(element+"="+document[element])
<br>}
<br></script> (the for loop is the most important one in control)