Reading this article requires programming experience in other languages.
Before you start studying
Most programming languages have a good part and a bad part. This article only covers the good parts of JavaScript because:
1. Just learning the good part can shorten the study time
2. Code written more robust
3. Code written is more readable
4. Code written is easier to maintain
Weak type and strong type
In general, the sooner you fix the error, the less you pay for it. Compilers of strongly typed languages can check for certain errors at compile time. JavaScript is a weakly typed language and its interpreter cannot check for type errors, but practice shows that:
1. Strongly typed errors that can be avoided are not critical errors
2. Weak types can provide flexibility without the need to carry a strong type of baggage
JavaScript related standards
The ECMA-262 standard defines the language ECMAScript. The JavaScript and ActionScript we are familiar with are based on ECMAScript. The current mainstream use of ECMA-262 Fifth Edition, Google's V8 engine is to achieve this.
Hello JavaScript
JavaScript is a scripting language that requires an interpreter to interpret execution. You can interpret JavaScript in a browser or use Node.js,node.js to integrate Google's V8 JavaScript engine. Since Node.js is very convenient to use, here I use node.js to explain the execution of JavaScript. Now look at the first JavaScript program:
Copy Code code as follows:
Test.js
Console.log ("Hello JavaScript");
Execute this program:
Copy Code code as follows:
Grammar
Comments
JavaScript uses the same annotation method as C + + for single-line comments,/*//for multiline comments.
Number Type
JavaScript has only one numeric type, which is a 64-bit floating-point number. Numeric types have two special values NaN and Infinity,nan mean not a number (not a few), using function isNaN to check for Nan, and value Infinity represents infinity. In the Math object, there is a set of methods for manipulating numbers, for example: The Math.floor method is used for rounding down.
String
string literal can be wrapped in single or double quotes, and escape characters are used \ (no different from many other languages). Each character in JavaScript is two bytes, and it uses the Unicode character set. String has a length property:
Copy Code code as follows:
"Hello". Length//value is 5, note not "hello". Length ()
Strings cannot be changed (as in Lua), except for the length attribute here, there are some methods, such as:
Copy Code code as follows:
' Cat '. toUpperCase () = = ' Cat '
Statement
The Var statement is used to declare a local variable, otherwise the variable is a global variable, and the value of the uninitialized variable is undefined:
Copy Code code as follows:
function f () {
var localvar = 123;
Globalvar = 456;
var i; The value of I is undefined
};
f ();
Console.log (Globalvar); Ok
Console.log (Localvar); Error, Localvar not defined
A set of statements wrapped by {} is called a statement block, and unlike other languages, functions in JavaScript will not create new scopes, such as:
Copy Code code as follows:
{
var v = 123;
}
Console.log (v); Ok
If statement
Copy Code code as follows:
if (expression)
Statement
Or
Copy Code code as follows:
if (expression)
Statement1
Else
Statement2
Or
Copy Code code as follows:
if (expression1)
Statement1
else if (expression2)
Statement2
else if (Expression3)
Statement3
Else
Statement4
An If statement determines whether to execute or skip some statements by determining whether the value of an expression is true or false. The following values are false in JavaScript (all other values are true):
1.false
2.null
3.undefined
4. Empty string
5.0
6.NaN
The statement in if can be either a statement or a block of statements.
Switch statement
Copy Code code as follows:
Switch (n) {
Case 1://if n equals 1
Execute code block
Break
Case 2://if n equals 2
Execute code block
Break
Default://If n is not 1 or 2
Execute code block
Break
}
The break here is used to exit the loop statement or the switch statement. In JavaScript, comparing two values is equal to two operators:
1.== (corresponding to the!= operator), equal, two operand types are not the same, this operator attempts the operand type conversion before comparing, for example:
Copy Code code as follows:
var x = 1;
x = = 1; True
x = = "1"; True
2.=== (corresponding to the!== operator), exactly equal, compares two operands, does not perform operand type conversion, for example:
Copy Code code as follows:
var x = 1;
x = = 1; True
x = = = "1"; False
Note that Nan and any values are not equal, and if X is Nan, then x!== x (only for Nan), we can implement the isNaN function as follows:
Copy Code code as follows:
function isNaN (n) {
return n!== N;
}
The switch statement above converts to an if statement:
Copy Code code as follows:
if (n = = 1)
// ...
else if (n = = 2)
// ...
Else
// ...
While and Do-while statements
Copy Code code as follows:
while (expression)
Statement
If expression is true, repeat execution statement until expression is false.
Copy Code code as follows:
Todo
Statement
while (expression);
Like a while loop, just perform the statement first, then check the condition expression.
For statement
Copy Code code as follows:
for (initialize; test; increment)
Statement
First Initialize is executed once (commonly used to initialize the loop variable), then test condition tests (commonly used to test the loop variable), if the test condition is false stop the loop, otherwise execute the statement, and then execute the increment (commonly used to update the loop variable), The test condition is then tested, so the loop executes. Use Example:
Copy Code code as follows:
for (var i=0; i<5; ++i) {
Console.log (i);
}
A different form of for is used to enumerate all the property names of an object:
Copy Code code as follows:
For (variable in object)
Statement
Example:
Copy Code code as follows:
var obj = {
A:1,
B:2,
C:3
};
for (var name in obj)
Console.log (name);
It should be noted that the hasOwnProperty method is used to check whether the property name is the object or is found in the prototype chain (prototype Chain,prototype will be introduced in the next article):
Copy Code code as follows:
for (var in obj) {
if (Obj.hasownproperty (Var)) {
// ...
}
}
Return statement
The return statement is used to return a value to a function, and if the function does not explicitly use returns, go back to undefined:
Copy Code code as follows:
function f () {}
var v = f (); v = = undefined
?: Condition operator (one ternary operator in JavaScript only)
?: Conditional operators exist in many programming languages, and when the first operand is true, the operator returns the value of the second operand, otherwise the value of the third operand is returned, using the example:
Copy Code code as follows:
function abs () {
return x > 0? x:-X;
}
typeof operator
The typeof operator is used to get the type of the variable whose return value includes:
1. ' Number '
2. ' String '
3. ' Boolean '
4. ' Undefined '
5. ' function '
6. ' Object '
The result of a special typeof null return is ' object '. Examples of typeof:
Copy Code code as follows:
var a = typeof ' Hello '; A = = = ' String '
var b = typeof null; b = = ' object '
+ operator
The + operator can be used for addition operations in JavaScript, or for string concatenation:
Copy Code code as follows:
var message = ' Hello ' + ' world '; message = = = ' HelloWorld '
&& and | | Operator
The && operator returns the value of the first operand when the first operand is false, otherwise the value of the second operand is returned
|| operator returns the value of the first operand when the first operand is true, otherwise the value of the second operand is returned
Copy Code code as follows:
var a = 1 && true; A = = True
var b = 1 | | False b = = 1
|| A customary use of:
Copy Code code as follows:
Name = Name | | ' Unknown '; Set default value ' unknown ' for name