Javascript is really not difficult-review the basic knowledge

Source: Internet
Author: User

Preface
Before talking about Jquery, let's take a look at the basic knowledge issues in Javascript (later referred to as JS) language. At that time, the basic knowledge for each programming language is similar, for variables and functions, conditional statement blocks, cyclic statement blocks, and so on. Each language is different in writing. For example, when defining variables in JS, you must declare local variables using var, for the weak type language JS, you can also ignore var, but without it, you will think that this variable is a global variable. This is worth noting.

Variable
The value can change when the program is running (haha, as defined in the book more than 10 years ago)
Var people; // declare a variable named people
Var people = "good boy"; // assign a value to a variable while declaring it. The browser automatically interprets it as a variable of the variable type.
Var age = 23; // declare a local variable, whose type is integer.
Age = 30; // declare a global variable of the type of integer. It will not be released during program execution.

Function
To implement a function, some code blocks are organized together to form a whole. We call it a function, which can greatly reduce the amount of code duplication and make the program clearer.
Copy codeThe Code is as follows:
// Standard writing
Funciton helloFun (){
Alert ("hellow world ")
}
// Variable format
Var helloFun = function (){
Alert ("hellow world ")
}
// The function can have parameters, which are of the weak type
Var helloFun = function (msg ){
Alert (msg );
}
// Call a function
HelloFun ("hello world ");

Condition Statement
If there are multiple results for one thing, the condition statement will appear. if the condition is fixed with several values, you can use switch. Otherwise, you can use if... else to check the code.
Copy codeThe Code is as follows:
// Switch instance
Var inputNumber = document. getElementByID ("type ");
Switch (inputNumber)
{
Case 1:
Alert ("Type 1 ");
Break;
Case 2:
Alert ("Type 2 ");
Break;
Case 3:
Alert ("Type 3 ");
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 ("Elderly ")
}
Else
{
Alert ("incorrect form filling ");
}

Loop statement
That is, when a condition is attached, a code block is repeatedly executed. We can use while, for, and so on.
Copy codeThe Code is as follows:
// For Loop
Var arr = [1, 2, 3]
For (int I = 0; j = Arr. length; I <j; I ++ ){
Console. log (arr [I]); // you can see the result on the Firefox console.
}

In fact, in the JS world, the execution performance of code is also very exquisite. Our Writing Performance in the for statement is not wrong, but if it is written below, the performance will decrease, because every time
The length of the Arr is evaluated.
Copy codeThe Code is as follows:
// For Loop
Var arr = [1, 2, 3]
For (int I = 0; I <Arr. length; I ++ ){
Console. log (arr [I]); // poor performance
}

Now, I will write down the basic JS programming knowledge here. Starting from the next time, I will introduce jquery-related knowledge. Thank you for reading this article!

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.