JavaScript basic syntax

Source: Internet
Author: User
Tags bitwise operators logical operators script tag

"Three ways to use JS"
1, in the HTML tag, direct use of JS (do not promote the use of):
Chestnut: <button onclick= "alert (' I'm Good ')" > Point Ben </button>
Does not meet the requirements of separation of content and behavior!

2. Use the <script></script> package JS code in the HTML page:
Chestnut: <script type= "Text/javascript" >
JS Code;
</script>
The script tag can be placed anywhere on the page.

3, the introduction of external JS file
<script language= "JavaScript" src= "Js/01.js" ></script>

"Precautions"
1, <script></script> can be embedded in any position of the page. However, the different positions will lead to different execution order of JS code;
For example,:<script></script> put in front of <body></body>, then the JS code will be executed before the page loads.
2, the introduction of external js,<script></script> must be paired with the appearance of the label. Also, there can be no JS code in the tag.

Multiple lines of comments in JS. ctrl+shift+/
A single-line comment in JS. ctrl+/


    "js in variable"
   1, JS the wording of the variable declaration:
       var num=1;//uses VAR declared variables, which are local variables, valid only at the current scope, and
      num=1;//variables without var declaration. The default is the global variable, which is valid throughout the JS file.
      var x=1,y=2,z=1;//uses a single line of statements to declare multiple variables, in the following form, X, Y is declared, but not assigned, and the result is undefined;
       var x,y,z=1;
      alert (x);
      alert (y);
      alert (z);


"Declaration of variables considerations"
1, the Declaration of all variables in JS, all use the var keyword, the variable is exactly what data type, depending on the type of assignment to the variable.
2, the same variable, you can change the data type of the variable at multiple different assignments;
var a=1;//from the initial declaration, a belongs to the plastic
A= "Chuanchuan";//The integer type A is modified to be a string type when repeated assignment;
3, variables can use VAR declaration, can also not be useful var declaration.
"Difference" uses VAR declaration as a local variable, not using var declaration as global variable;
4, only with VAR declaration, but not assigned value, the result is undefined;
For example, Var a;//a is undefined.
However, if you do not declare and do not assign a, direct use will give an error.
5, the same variable name can be used multiple times, but the subsequent Var is not used, the second time using the Var declaration, will only be understood as a normal assignment operation.

"Naming requirements for variable names"
1, can only have letters, underline, the number of components;
2, the beginning can not be a number;
3, variable names are case-sensitive, uppercase and lowercase letters are different variables;


"Naming conventions for variable names"
1, to conform to the small hump rule: The first letter lowercase after the first letter of each word capitalized.
Example: Mynameischuanchuan
2, or use the Hungarian name of life law
My_name_is_chuan_chuan
3, Mynameischuanchuan can be used, but not standardized

"JS data type"
1, Undefined: Not defined. The Var gods have been used, but there is no value assigned. such as: Var A;
2, NULL: null reference.
3, Boolean: Boolean type. Denotes true and false, only two values, real and flase
4, Nnumber: Numeric type, can be an integer, or it can be a decimal
5. String: The literal type, with "" or "wrapped content, called string
6. Object: Type,

"Common numeric Functions"
1, IsNaN (): Determine whether a variable or constant is Nan (not a number non-numeric);
When used with Nan (), an attempt is made to convert using the number () function, which is not nan if the final result can be converted to a digit, and the result is false.

2. Number () function: Attempts to convert other types of data to numeric.
[String Type]
The string is a pure numeric string and is converted to a corresponding number; for example: "111"->111
The string is an empty string and is converted to 0; for example: ""->0
String contains any other string, it cannot be transferred; for example: "1a"->nan
[Boolean type]
True->1 flase->0
"Null/undefined"
Null->0 Undefined->nan

parseint (): Converts a string to an integer type;
A pure numeric string that can be turned.
"->12;" "12.9"->12 (decimal conversion, directly erase the decimal point, cannot be rounded off)
Empty string, cannot go. ""->nan
Contains other strings that intercept the number part before the first non-numeric string
"123a456"->123; "A123b456"->nan
parseint () can only be transferred to a string. Go to other types, all Nan.

"number function differs from parseint function"
1, number function can go to various data types, parseint function can only go to string
2. The results are not exactly the same when the two strings are turned. (See explanation above)

Parsefloat: Converts a string to a numeric type;
The conversion rule is the same as parsenint, except that if there are decimals, the decimal point is preserved, and if there are no decimals, it remains an integer;
"12.5"->12.5; "->12"

typeof: The data type of the detection variable:
String->string numeric->number undefined->undefined
True/false->boolean function->function Object/null->object

"input and output statements commonly used in JS"
1, document.write (); Print the contents of parentheses to the browser screen;
Note: All content except variables, constants, must be placed in "", variables and constants must be placed outside the "";
If you have variables and strings at the same time, you must use the + link
For example: document.write ("Solitaire in the left hand:" +left+ "<br/>");
2. alert (); Use pop-up window output;
The contents of the pop-up Window warning, () are the same as the above requirements.
3, prompt (); pop-up window input
Accept two-part parameters:
① the input box above the prompt content, optional;
② The default information in the input box, optional;
When only a portion is written, the input content is represented. Click the OK button, the variable is assigned to the input content, click the Cancel button, the variable will be assigned to NULL;
When you enter content, the data type that is received by default is a string!
For example: var num1 = prompt ("Please enter the first number:");
var num2 = prompt ("Please enter a second number:");
var sum = parsefloat (NUM1) +parsefloat (num2);
Alert ("The result is:" +sum);

"Arithmetic operations"
+ 、-、 *,/,%, + + 、--
+: There are two kinds of functions, link string/addition operation. When the + sides are all numbers, the addition operation is performed, and when the + side is a string,
Link string function, link function, link after result is a string.
In addition to +, the left and right variables are converted to numbers using the number function when the remaining symbols are operational.

/: The result retains the decimal point.
+ +: The increment operator, the variable is based on the original +1.
--: The decrement operator, the variable on the original basis-1.

"a++ and ++a similarities and differences"
1, the same point: whether a++ or ++a, after the operation, the value of a will be +1;
2, different points: a++, first use the value of a to calculate, and then put a+1;
++a, then put a+1, and then use a value to calculate;

"Assignment Operation"
=  +=  -=  *=  /=  %=
+=:a+=b is equivalent to A=a+b, but the former is more efficient and recommends the notation of + =.

"Relational Operations"
= =,! =, >, <, >=, <=
The result after the relational operator operation can only be a Boolean type;
To determine whether a number is in a certain range, you must use the && link;
Example: A<10 && a>0
= = = Strictly, requires not only the same type, the value must also form the same.
= = equals, the same type as the = = = Effect, the type is not the same, will try to use number to turn the two sides, and then judge.
However, there are individual exceptions, such as: NULL==FALSEXNULL==UNDEFINED√NULL==NULLX

"conditional operator (multi-mesh operation)"
A>b?true:false
When? When the preceding operation evaluates to True, execute: The preceding code
When? When the result of the subsequent operation is true, execute: The following code
If the colon is a numeric value, the entire formula can be used for assignment. var a =1<2?1:2;
A number on either side of the colon executes the code directly. 1<2? Alert (1): Alert (2);
Multi-mesh operators can be nested in multiple layers. var a =1<2?alert (1):(1>0?4:5);

bitwise operators, logical operators
&&, | |,!
&& Both sides are set, the result is true
|| On either side of the set, the result is true
&& | | The priority ratio of,&& when it exists at the same time | | High

"Precedence of Operators"
1. ()
2,! + +--monocular operator
3, */%
4, > < >= <=
5. = = =
6, && Note:&& | | The priority ratio of,&& when it exists at the same time | | High
7, | |
8, = + = = *=/=

"If-else structure"
1, the structure of the wording:
if (judging condition) {
Condition is true, execute if{}
}else{
Condition is False, execute if{}
}
2. Precautions:
else{} statement block. can be omitted according to the situation.
{} After if and else can be omitted, but omitting {},if and else follows only one statement; (so it is not recommended to omit {})

3. If the judgment condition in the (), support the situation:
Boolean:true is True, False is False
String: The empty string is false, and all non-empty strings are true.
Number:0 is false. All non-0 digits are true.
Null/undefined/nan: All is false.
Object: All is true.

"Multiple if, ladder if structure"
1, the structure of the wording:
if (if condition i) {
When the condition is established, the operation is performed
}else if (condition two) {
Condition one not established && condition II established, operation performed
}else{
Conditions are not set, the actions performed
}
2, multiple if structure, each judgment condition is mutually exclusive, execution chooses one of the road execution,
After encountering the correct options and executing, jump out of the structure and no longer judge the subsequent branch

"Nested IF Structure"
1, the structure of the wording:
if (condition i) {
Conditions established
if (condition two) {
Condition I set up && condition II established
}else{
Condition One establishment && condition two not established
}
}else{
Condition one is not tenable
}
2. If the {} is omitted in the nested if structure, then the else structure will always belong to the nearest if;
3, nested structure can be nested multiple layers, but generally not recommended more than three layers
Multiple if structures are generally not recommended for use with multiple if structures.

"Switch multi-branch structure"
"How to Work" first sets the expression n (usually a variable). The value of the subsequent expression is compared to the value of each case in the structure.
If there is a match, the code block associated with the case is executed. Use break to prevent your code from automatically running down a case.
1. Structure notation: switch (expression) {
Case (can be an expression):
Executing code block 1
Break
Case 2:
Executing code block 2
Break
Case 3:
Executing code block 3
Break
Default
Break
}
2, the switch structure () of the expression can be a variety of JS support data type;
3, switch structure in the judgment, use = = judgment;
4, the expression after the case can be a variety of data types, can be an expression, but the value requirements are different, otherwise only the first case will be executed.
5. Break jumps out of the current switch structure; the absence of a break consequence starts with the first correct case item and executes all subsequent case and default;
Reason: When a switch structure is judged, it will only judge the correct answer once. When the correct case is encountered, subsequent items are no longer judged.
6, switch execution efficiency is faster than multiple if. In a multi-channel branch, it is recommended to use first.

JavaScript basic syntax

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.