JavaScript Beginner Tutorial (second lesson) 1th/7 Page _ Basics

Source: Internet
Author: User
In the last lecture we learned where JavaScript appears and what it looks like. Now we begin to learn the language. In this lesson we'll learn how JavaScript stores information, how to make decisions based on information, how to alternate pictures based on user interaction requirements
You ready? Now start learning the basics of computer programming. First, variables.
If you have studied algebra, you must have seen variables. It doesn't matter if you haven't learned it. Variables are a simple way for JavaScript to store information. For example, when you write: "x=2," "X" is a variable that stores a value of 2. If you then say "y=x+3," then "Y" will have a value of "5"
Here is a JavaScript example that uses variables.
In this example we step through the source code you will see:
<script language= "JavaScript" >
<!--hide Me
We have seen these two lines in the beginning. This is the prelude that any JavaScript program must have.
Load up some variables
var secs_per_min = 60;
var mins_per_hour = 60;
var hours_per_day = 24;
var days_per_year = 365;
The first line is a comment. The meaning is clear.
The next few lines are variable declarations, and there are several things to note:
When a variable is first used, it should be declared as "Var".
Although using Var as a variable is strictly not necessary, it is a good habit. When we discuss the next two hours, we will know why.

The variable must start with a letter or accent character.
After the first character, the variable can have a number. So monkey_23 is the legal variable name.
Variable names are mostly case sensitive, but not necessarily for different versions of JavaScript.
This means that the variable loop and loop are different in some browsers. In general, it is recommended that you persist in using a naming convention and remain unchanged. I myself
Get used to underlining between variables. Other people have a preference for capital letters, such as secspermin, between variables.
Variables should describe their purpose.
Like X, Y, or hack_hack_hack is of no use to anyone who wants to know your language. Don't make variable names too long to knock harder, but long
To have a certain amount of descriptive power.
You can assign a value to a variable when you define it, or you can give it in the future.
In this example, each variable is assigned a value when it begins to be defined. Not necessarily so, in future examples we will see that even if we do not know its value, I
We can also define it very well.

The statement ends with a semicolon.
A statement is a JavaScript expression and a semicolon is a closing flag. Spaces and blank lines can be ignored by the JavaScript compiler, and they are only for people to read
Handy, this example can be written in a long line, but even with the annotation, it looks extremely laborious.
Before I conclude, I want to mention that sometimes half brackets are not necessary, and you may also notice that some people make statements without semicolons as the end. But plus it's a good
Habits, this is not only because your program's readability will be much better, this can also reduce the empty line, these empty lines stuffed with your program Webmonkey habit is
After each statement, insert a half bracket as the end.
Do some calculations
var secs_per_day = secs_per_min * Mins_per_hour * hours_per_day;
var secs_per_year = Secs_per_day * days_per_year;
Here we see some basic arithmetic. Whenever JavaScript executes the statement, the variable secs_per_year will be multiplied by 60, 60, 24, and 365 regardless of what number is obtained. Later, whenever a variable secs_per_year is encountered, it will be replaced with these huge numbers.
End Hiding-->
</script>
There's nothing new here, it's the end of a JavaScript movie.
This is all of the JavaScript in the header file in the example, and when JavaScript executes the code, the above variables are defined. But at this point these variables have not done anything, and this is what is to be done in the example body.
Current 1/7 page 1234567 Next read the full text

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.