Home > Article > Web Front-end > JavaScript minimalist introductory tutorial (1): Basics_javascript skills
Reading this article requires programming experience in other languages.
Before you start studying
Most programming languages have good parts and bad parts. This article only covers the good parts of JavaScript because:
1. Only learning good parts can shorten the learning time
2. The code written is more robust
3. The code written is more readable
4. The code written is easier to maintain
Weak typing and strong typing
Generally speaking, the sooner a bug is fixed, the less costly it is. Compilers for 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:
1. The errors that strong typing can avoid are not critical errors
2. Weak typing can bring flexibility, and there is no need to carry the baggage of strong typing
JavaScript related standards
The ECMA-262 standard defines the language ECMAScript. The JavaScript and ActionScript we know well are both based on ECMAScript. Currently, the mainstream uses ECMA-262 fifth edition, 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 interpret and execute JavaScript in the browser or directly use node.js, which integrates Google's V8 JavaScript engine. Since node.js is very convenient to use, here I use node.js to interpret and execute JavaScript. Now look at the first JavaScript program:
Execute this procedure:
Grammar
Notes
JavaScript uses the same comment method as C, // is used for single-line comments, and /* */ is used for multi-line comments.
Number type
JavaScript has only one number type, which is a 64-bit floating point number. The numeric type has two special values, NaN and Infinity. NaN means not a number (not a number). Use the function isNaN to check whether it is NaN. The value Infinity means infinity. In the Math object, there are a set of methods for manipulating numbers, for example: the Math.floor method is used to round down.
String
String literals can be wrapped in single or double quotes, using escape characters (not unlike many other languages). Each character in JavaScript is two bytes and uses the Unicode character set. Strings have a length property:
Strings cannot be changed (same as Lua). In addition to the length attribute mentioned here, there are also some methods, such as:
Statement
Thevar statement is used to declare local variables, otherwise the variable is a global variable, and the value of an uninitialized variable is undefined:
A group of statements wrapped by {} is called a block. Unlike other languages, functions in JavaScript will create new scopes but blocks will not, for example:
if statement
or
or
The if statement determines whether to execute or skip certain statements by judging whether the value of the expression is true or false. In JavaScript the following values are false (all 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 break here is used to exit the loop statement or switch statement. In JavaScript, there are two operators to compare whether two values are equal:
1.== (corresponding to != operator), equal, when the two operand types are different, this operator attempts to convert the operand type before comparison, for example:
2.=== (corresponding to !== operator), completely equal, comparing two operands without performing operand type conversion, for example:
It should be noted that NaN is not equal to any value. If x is NaN, then x !== x (only true for NaN), we can implement the isNaN function like this:
The above switch statement is converted into an if statement:
while and do-while statements
If expression is true, statement is executed repeatedly until expression is false.
Similar to a while loop, except that statement is executed first and then the conditional expression is checked.
for statement
First initialize is executed once (commonly used to initialize loop variables), and then the test condition is tested (commonly used to test loop variables). If the test condition is false, the loop is stopped, otherwise statement is executed, and then increment is executed (commonly used to update loops) variable), and then perform the test condition test, and the loop continues. Usage example:
Another form of for is used to enumerate all property names of an object:
Example:
return statement
Thereturn statement is used to let the function return a value. If the function does not explicitly use return, then undefined is returned:
?: conditional operator (the only ternary operator in JavaScript)
?: The conditional operator exists in many programming languages. When the first operand is true, the operator returns the value of the second operand, otherwise it returns the value of the third operand. Usage example:
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 special typeof null returns 'object'. Example about typeof:
Operator
The operator can be used for addition operations in JavaScript and can also be used for string concatenation:
&& and || operators
&& operator returns the value of the first operand if the first operand is false, otherwise returns the value of the second operand
The || operator returns the value of the first operand if the first operand is true, otherwise it returns the value of the second operand
||: