Overview
JavaScript is described by the ECMAScript Pseudo-language, ECMA-262 3rd version is currently the most browser implementation version, the 5th version is the future version of the browser to implement
Grammar style
JavaScript syntax style Class C language, case-sensitive
Identifier
Identifiers are the names of variables, functions, and attributes, and the naming rules for identifiers in JavaScript are as follows:
① first character must be a letter or underscore or dollar sign
② Other characters can be letters, underscores, dollar signs
③ identifiers are in hump case format, that is, the first letter is lowercase, and the first letter of each meaningful word is capitalized, such as Firstchar
Annotation methods
Because of the JavaScript language class C, it also uses the C-language annotation style, including single-line comments and multiline comments
1 // single-line comment 2 3 /* Multi-line comments 4 5 6 */
Strict mode
ECMAScript introduced the strict mode in the 5th edition. In strict mode, JavaScript performs a different parsing and execution model, and some of the previously indeterminate behavior will be handled or incorrectly prompted. You can use strict mode throughout the script, or you can use strict mode in the specified function, just put "use strict" in front of them
Variable
According to the ECMA-262 definition, a JavaScript variable is a weak type, which is simply a name that is used to hold a particular value at a specific time. Both the value of the variable and its data type may change during execution, that is, variables in JavaScript can be used to hold any type of data, as defined by the variable:
1 var somevalue; // undefined value is saved by default without initialization 2 var // define variables and initialize them directly to Hello world! 3 var messages=120; // change the value and type of a variable
PS: Variables defined using the var operator belong to local variables
Variable type
The value of the ECMAScript variable is divided into two types: the base type value and the reference type value
Base type value: Simple data segment
Reference type value: An object consisting of multiple values
The data types that store the base type values are: Undefined, Null, Boolean, number, String
Ps:javascript does not support any custom types
The difference between a base type value and a reference type value:
① reference type values can add properties dynamically, while primitive type values cannot be
② the base type value is copied when a new copy is created for assignment, and the reference type value copies a pointer or points to the original value
③ when passing parameters to a function, the assignment is in the same way as the variable assignment principle
Detection type
Because JavaScript is loosely typed, a mechanism is required to detect the type of a given variable, in two ways:
typeof operator for basic type detection
instanceof operator for reference type detection
Undefined type
Undefined type, only one value is undefined, when you use Var to define a variable but do not initialize it, the default value of this variable is undefined value. It is important to note that variables that contain undefined values are not the same as those that have not been defined. Only one operation is allowed for variables that have not yet been defined, that is, the TypeOf action is used to detect its type, but the typeof operator is executed for variables that have not yet been defined and uninitialized, and they return the same results, all of which are undefined values
Null type
A null type, and only one value, or null. A null value is logically represented as an empty object pointer. So when it detects the type, it returns "Object".
var car=null; Alert (typeof car);
If you define a variable that will be used to store the object in the future, it is best to initialize it to null
Boolean type
The Boolean type is the most used type in JavaScript and it has only two values: True and false, but they are not necessarily equal to 1 and 0
Although the Boolean type has only two values, all of the types in JavaScript can be converted to the equivalent of this two value, and you can call the transformation function to complete
You can call the Transform function Boolean () on a value of any data type, and the result depends on the data type and the actual value you want to transform, as follows:
Data type |
The value converted to true |
Value converted to False |
Boolean |
True |
False |
String |
Any non-empty string |
Empty string |
Number |
Any non-0 value (including infinity) |
0 or Nan |
Object |
Any object |
Null |
Undefined |
N/A (not practical) |
Undefined |
JavaScript Basic Concepts