Reading this article requires programming experience in other languages.
Before starting to study
Most programming languages have good parts and bad parts. This article only talks about the good parts of JavaScript, because:
1. Just learning the best part can shorten learning time
2. The code written is more robust
3. The code written is easier to read
4. The code written is easier to maintain
Weak and strong types
Generally speaking, the earlier you fix the bug, the less the price you pay for it. Compilers of strongly typed languages can check for certain errors at compile time. JavaScript is a weak-type language, and its interpreter cannot check for type errors, but practice shows that:
1. The errors that strongly typed can avoid are not those critical errors
2. Weak types can bring flexibility without having to bear the burden of strong types
JavaScript-related standards
The ECMA-262 standard defines the language ECMAScript. JavaScript and ActionScript as we are familiar with are all based on ECMAScript. Currently, the mainstream uses ECMA-262 version 5, and Google's V8 engine is the implementation of this.
Hello JavaScript
JavaScript is a scripting language that requires an interpreter to interpret and execute. You can explain JavaScript in the browser or directly use node.js, which integrates Google's V8 JavaScript engine. Since node.js is very convenient to use, I use node.js here to explain the execution of JavaScript. Now look at the first JavaScript program:
The code copy is as follows:
// test.js
console.log("Hello JavaScript");
Execute this program:
The code copy is as follows:
node test.js
grammar
Comments
JavaScript uses the same annotation method as C++, // for single-line annotation, /* */ for multi-line annotation.
Number Type
JavaScript has only one type of numerical type, which is a 64-bit floating point number. The number type has two special values, NaN and Infinity. The meaning of NaN is not a number (not a number). Use the function isNaN to check whether it is NaN. The value Infinity means infinity. In a Math object, there is a set of methods for manipulating numbers, for example: the Math.floor method is used to round down.
String
The string literal can be wrapped in single or double quotes, and escaped characters are used (no different from many other languages). Each character in JavaScript is two bytes, which uses the Unicode character set. A string has a length attribute:
The code copy is as follows:
"Hello".length // The value is 5, note that it is not "Hello".length()
Strings cannot be changed (like Lua). In addition to the length attribute mentioned here, there are some methods, such as:
The code copy is as follows:
'cat'.toUpperCase() === 'CAT'
Statement
The var statement is used to declare local variables, otherwise the variable is a global variable, and the value of the uninitialized variable is undefined:
The code copy is 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 is not defined
A set of statements wrapped by {} is called a statement block. Unlike other languages, functions in JavaScript will not create new scopes, for example:
The code copy is as follows:
{
var v = 123;
}
console.log(v); // OK
if statement
The code copy is as follows:
if (expression)
statement
or
The code copy is as follows:
if (expression)
statement1
else
statement2
or
The code copy is as follows:
if (expression1)
statement1
else if (expression2)
statement2
else if (expression3)
statement3
else
statement4
The if statement decides to execute or skip certain statements by judging that the value of the expression is true or false. In JavaScript, the following values are false (other values are true):
1.false
2.null
3.undefined
4. Empty string
5.0
6.NaN
The statement in if can be a statement or a statement block.
switch statement
The code copy is as follows:
switch (n) {
case 1: // If n is equal to 1
// Execute code block
break;
case 2: // If n is equal to 2
// Execute code block
break;
default: // If n is not 1 nor 2
// Execute code block
break;
}
Here break is used to exit a loop statement or switch statement. In JavaScript, there are two operators that compare whether two values are equal:
1.== (corresponding to != operator), equal, and the two operand types are different. This operator tries to convert the operand type before comparing, for example:
The code copy is as follows:
var x = 1;
x == 1; // true
x == "1"; // true
2.=== (corresponding to !== operator), which is completely equal, compares two operands, and does not perform operand type conversion, for example:
The code copy is as follows:
var x = 1;
x === 1; // true
x === "1"; // false
It should be noted that NaN and any value are not equal. If x is NaN, then x !== x (only holds for NaN). We can implement the isNaN function like this:
The code copy is as follows:
function isNaN(n) {
return n !== n;
}
The above switch statement is converted into if statement as:
The code copy is as follows:
if (n === 1)
// ...
else if (n === 2)
// ...
else
// ...
while and do-while statements
The code copy is as follows:
While (expression)
statement
If expression is true, repeat statement until expression is false.
The code copy is as follows:
do
statement
while (expression);
Similar to a while loop, it just executes the statement first and then checks the conditional expression.
for statement
The code copy is as follows:
for (initialize ; test ; increment)
statement
First, initialize is executed once (usually used to initialize loop variables), then test condition test (usually used to test loop variables), if the test condition is false, the loop stops, otherwise the statement is executed, and then increment (usually used to update loop variables), and then test condition test is performed, so the loop continues. Example of usage:
The code copy is as follows:
for (var i=0; i<5; ++i) {
console.log(i);
}
Another form of for is used to enumerate all attribute names of an object:
The code copy is as follows:
for (variable in object)
statement
example:
The code copy is as follows:
var obj = {
a: 1,
b: 2,
c: 3
};
for (var name in obj)
console.log(name);
It should be noted that we use the hasOwnProperty method to check whether the property name is of the object or found in the prototype chain (prototype chain, prototype will be introduced in the next article):
The code copy is as follows:
for (var in obj) {
if (obj.hasOwnProperty(var)) {
// ...
}
}
return statement
The return statement is used to make the function return a value. If the function does not explicitly use return, then it returns undefined:
The code copy is as follows:
function f() { }
var v = f(); // v === undefined
?: Conditional operator (the only ternary operator in JavaScript)
?: Conditional operators exist in many programming languages. When the first operand is true, the operator returns the value of the second operand, otherwise the value of the third operand is returned. Example:
The code copy is as follows:
function abs() {
return x > 0 ? x : -x;
}
typeof operator
The typeof operator is used to obtain the type of a variable, and its return value includes:
1.'number'
2.'string'
3.'boolean'
4.'undefined'
5.'function'
6.'object'
The result returned by the special typeof null is 'object'. Examples about typeof:
The code copy is as follows:
var a = typeof 'hello'; // a === 'string'
var b = typeof null; // b === 'object'
+ Operator
The + operator can be used in JavaScript for addition operations or string concatenation:
The code copy is as follows:
var message = 'hello' + 'world'; // message === 'helloworld'
&& and || operators
The && operator returns the value of the first operand when the first operand is false, otherwise it returns the value of the second operand.
|| The operator returns the value of the first operand when the first operand is true, otherwise it returns the value of the second operand.
The code copy is as follows:
var a = 1 && true; // a === true
var b = 1 || false; // b === 1
|| An idiom:
The code copy is as follows:
name = name || 'unknown'; // Set the default value 'unknown' for name