First, the grammar
------------------------------
Everything in JavaScript (variables, function names, and operators) is case-sensitive.
Identifier: Refers to the name of the variable, function, property, the identifier is required as follows
1 The first character must be a letter, an underscore (_), or a dollar sign ($)
2 other characters can be letters, underscores, dollar signs, or numbers
3 cannot use keywords, reserved words, true, false, NULL as identifiers
Comments
Single-line Comment
/*
*
* Multi-line Comment
*
*/
Strict mode
Strict mode is ES5 introduced, strict mode defines a different parsing and execution mode, in strict mode, some unsafe operation will run out of error
To enable strict mode throughout the script, you can add the following code at the top of the
"Use Strict"
You can also specify that the function executes in strict mode
function dosomething () {
"Use Strict"
// ....
}
Ii. variables and data types
------------------------------
Variable
1, the variable in the escmscript is loosely typed, that is, JavaScript makes the weakly typed language, in other words, the variable in JS is just a saved worthy placeholder, you can save any type of value.
2. Define variables with var operator.
3. A variable defined with the var operator becomes a local variable in the scope that defines the variable.
4. You can define global variables (not recommended) by omitting Var in function scope (block scope).
Data type
1, JS has 5 of the basic data types and 1 of complex data types they are
Undefined, Null, Boolean, number, string, and Object
2. The typeof operator is used to detect the data type of a variable, and using the TypeOf operator on a value returns a string that indicates the worthwhile type:
"Undefined"--if this value is undefined
"Boolean"--if this value is a Boolean value
"String"--if this value is a string
"Number"--if the value is numeric
"Object"--if this value is an object or null
"Function"--if this value is a function
3, JS 6 data type and use typeof to determine the data type does not correspond, the difference is that the use of typeof to get the type label is not "null", but more a "function", the others are the same, Also note that null is returned "object" when typeof is used, and the typeof operator gets a string.
Third, the statement
------------------------------
If statement
Grammar:
if (condition) {
}else if (condition) {
}else{
}
Explain:
Where the condition can be an arbitrary expression, and the result of the evaluation of the expression does not have to be a Boolean value, ES automatically calls the Boolean () conversion function to convert the result of the expression into a Boolean value.
While statement
Grammar:
while (condition) {
Something ...
}
Explain:
Where the condition is the same as the condition in the IF statement, the while statement is a pre-test loop statement that evaluates the conditional expression before executing the loop body, so the loop body may never execute.
Do-while statements
Grammar:
do{
Something ...
}while (condition)
Explain:
Where the condition is the same as the condition in the IF statement, the Do-while statement is a post-test loop statement, that is, the code in the loop body is executed at least once before the conditional expression is evaluated. The interesting notation is (while is the first to ask and then beat, Do-while is to beat up and then ask).
For statement
Grammar:
For (initialize code, condition, code to execute after loop body execution) {
Something ...
}
Explain:
The For statement is also a pre-test loop statement, so the loop body may never be executed, and the difference between a for statement and a while statement is the ability to initialize a variable and define the code to execute after the loop before executing the loop.
for-in statements
Grammar:
For (property in expression) {
Something ...
}
Explain:
The for-in statement is a precise iterative statement used to enumerate the properties of an object, and it is important to note that the order of the property names output by the For-in statement is not measurable, depending on the browser.
Switch statement
Grammar:
switch (expression) {
Case Value:
Statement
Break
Case Value:
Statement
Break
Case Value:
Statement
Break
Default:
Statement
}
Explain:
The switch statement is the closest process Control statement to the IF statement, and the switch statement in JavaScript has its own characteristics, first: You can use any data type in the switch statement, and second: the value of each case (that is, value), not just the constant, It can also be a variable or even an expression.
Label statement
Grammar:
Label:statement
Instance
Start:for (var i = 0; i < count; i++) {
Alert (i)
}
Explain:
You can use the Label statement to add tags to your code for future use, and the start tag in the example above can be referenced in the future by a break or continue statement so that the code jumps to the start of the tag execution, A generic label statement is used only with break or continue mates in a loop statement such as a for statement.
Break and Continue statements
Grammar:
Example
MyLabel:
for (var i = 0; i < i++) {
for (var j = 0; J <; J + +) {
if (i = = 5 && J = = 5) {
Break MyLabel;
Continue MyLabel;
}
}
}
Explain:
Both the break and the continue statements are used to jump out of the loop, except that the break statement enforces subsequent statements, and the continue statement simply jumps out of the loop and resumes the next loop. In the above example, the break statement and the label statement are used together to control the execution of the code.
With statement
Explain:
The function of the WITH statement is to set the scope of the code to a specific object. Using the WITH statement in strict mode will cause an error. The purpose of defining a with statement is to simplify multiple authoring of the same object, as in the following example:
var qs = location.search.substring (1);
var hostName location.hostname;
var url = location.href;
The above code contains the Location object, and the code can be simplified by using the WITH statement as follows
With (location) {
var qs = search.substring (1);
var hostName = HostName;
var url = href;
}
Iv. functions
------------------------------
Functions in JavaScript Use the function keyword to declare
function (arg0, arg1, arg2, ..., argn) {
Statements
}
Understanding Parameters
The parameters in JavaScript are characterized by:
Don't mind if you pass a few parameters, even if you define a function to receive a few parameters, it will not error.
It doesn't matter what data type you pass on the parameter.
This is because the parameters inside the JavaScript function are represented by a class array object, so the function always receives the class array object, and does not care what arguments it contains, and in the body of the function can use the arguments object to access the class array object. You can use the syntax of square brackets such as: arguments[0], arguments[1], etc., this fact illustrates an important feature:
Naming parameters is not required, it just provides convenience.
The length property of the arguments learns how many arguments are passed to the function:
function Howmanyargs () {
alert (arguments.length);
}
Howmanyargs ("string", 1); 2
Howmanyargs (); //0
Howmanyargs (1); //1
A bit more interesting about arguments is that its value is always synchronized with the corresponding formal parameter, that is, modifying the value of Arguments[0] will change the value of the corresponding named parameter accordingly. But this is not to say that they are accessing the same memory space, they have separate storage space, but the value will be synchronized, in addition, if the value is passed a parameter, modify the value of arguments[1] will not change the value of the corresponding named parameter, this should be, The length of the arguments.length is determined by the number of arguments passed, not the number of named parameters, which is emphasized once again, the naming parameter is just a convenience and has no other effect.
No overloads
Because a function in JavaScript does not have a feature of a function signature, there is no overload, but you can simulate overloading by checking the type and number of parameters passed in to the function to react differently.
JavaScript Basics Summary