JavaScript is really not difficult-review Basics _ Basics

Source: Internet
Author: User
Preface
Before you say jquery, first learn about the basics of JavaScript (hereafter referred to as JS) language, for each programming language, the basic knowledge is the same, for variables, functions, conditional statement blocks, circular block, and so on, and for each language in the writing to appear to be different , such as JS in the definition of variables, you want to use the var qusheng local variable declaration, and for the weak type of language JS, you can not add Var, but does not add it will think that the variable is a global variable, this is to note.

variable
In the process of running the program, its value can be changed (hehe, more than 10 years ago, the definition of the book)
var people; Declare a variable named people
var people= "Good Boy"; Declare a variable and assign it a value, and the browser automatically interprets it as a character variable
var age=23; Declare a local variable, type to reshape
age=30; Declares a global variable, the type is shaped, and is not released during program execution

function
In order to achieve a function, some code blocks are organized together to form a whole, we call it function functions, it can greatly reduce the amount of code duplication, and make the program clearer
Copy Code code as follows:

Standard wording
Funciton Hellofun () {
Alert ("Hellow World")
}
The formulation of variable form
var hellofun=function () {
Alert ("Hellow World")
}
A function can have arguments that differ in weak types
var hellofun=function (msg) {
Alert (msg);
}
Call to function
Hellofun ("Hello World");

Conditional Statement
For one thing, there are many results, then the conditional statement on the appearance, if the conditions fixed a few values, you can use switch, otherwise use If...else, see Code
Copy Code code as follows:

Switch instance
var Inputnumber=document.getelementbyid ("type");
Switch (inputnumber)
{
Case 1:
Alert ("Type 1th");
Break
Case 2:
Alert ("type 2nd");
Break
Case 3:
Alert ("type 3rd");
Break
Default
Alert ("throw new Exception ()");
Break
}
If instance
var Inputage=document.getelementbyid ("Age");
if (inputage>1 && inputage<18)
{
Alert ("minor");
}
else if (inputage>=18 && inputage<70)
{
Alert ("adult");
}
else if (inputage>=70)
{
Alert ("Senior Citizen")
}
Else
{
Alert ("Fill out the form incorrectly");
}

Loop Statement
That is, when we echo a certain condition, execute a code block repeatedly, we can use the while, the for etc.
Copy Code code as follows:

For loop
var arr=[1,2,3]
for (int i=0;j=arr.length;i<j;i++) {
Console.log (Arr[i]); Firefox console can see the results
}

In fact, in the JS World, the performance of the code is also very fastidious, we in the For statement writing performance is not wrong, but if written below, performance will be reduced, because it each time
When traversing, the length of the arr is to be obtained.
Copy Code code as follows:

For loop
var arr=[1,2,3]
for (int i=0;i<arr.length;i++) {
Console.log (Arr[i]); Bad performance.
}

OK, for JS programming basics to write here, starting from the next, will introduce jquery related knowledge, thanks for reading!
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.