Front end basics
Javascript basics
1. The variable
Note: 1. The first character must be a letter, underscore (_), or dollar sign ($) 2. The remaining characters can be underscores, dollar signs, or any alphanumeric or numeric characters. 3. Another interesting aspect of ECMAScript (and a major difference from most programming languages) is that variables do not have to be declared before they are usedCopy the code
Var scope, only function level scope and global scope:
Function-level scope: if a variable is defined with var in a function, the variable is a local variable in the scope defined by the variable and is destroyed after the function exits. Global scope: If var is omitted, it is a global variable that can be accessed anywhere outside the function (not recommended, difficult to maintain, and throws errors in strict mode); This is also the perennial problem of variable pollution, which can go wrong in strict mode;Copy the code
Let const
// var case console.log(foo); Var foo = 2; // let's case console.log(bar); // ReferenceError let bar = 2; In the above code, the variable foo is declared with the var command, and the variable promotion occurs. That is, when the script starts running, the variable foo already exists, but has no value, so undefined is printed. The variable bar is declared with the let command, and no variable promotion occurs. This means that the variable bar does not exist until it is declared, and an error will be thrown if it is used.Copy the code
The "variable promotion" phenomenon occurs when the var command is used, that is, the variable can be used before the declaration and its value is undefined. This is somewhat strange, as normal logic dictates that variables should be used only after the statement has been declared. To correct this, the let command changes the syntactic behavior so that the variables it declares must be used after the declaration or an error will be reported.Copy the code
2. Data type
Five types of data types that can contain values
String Type (string) Boolean Type (number) Object type (function)Copy the code
Three object types
Array (array) Date (Object)Copy the code
Two empty types
null
undefined
Copy the code
Note:
NaN's data type is numeric; The data type of an array is an object; Date is an object and NULL is an object; The data type of an undefined variable is undefined; The data type of an unassigned variable is also undefined. It is well known that JS contains values of two data types: basic data types and reference data types. Basic data types: null, undefined, number, string, Boolean; Reference data type: Object.Copy the code
Variable assignment
1. Basic data type values occupy a fixed amount of memory and are therefore stored in stack memory; Copying a value of a primitive type from one variable to another creates a copy of the value;Copy the code
let a = 123; let b = a; b = 333; console.log(a, b)// => a = 123; b = 333; The values of A and B reside at two addresses in memory, and do not affect each other.Copy the code
2. Reference-type data assignment: When assigning a value from a reference-type variable to another variable, the same applies, except that the reference-type value is actually a pointer to an object in the same heap as the original variable. Therefore, these two variables will affect each other.Copy the code
let a = {name: "linguomao"}; let b = a; b.name = "new"; b.city="cc"; // => {name: "new", city: "cc"} {name: "new", city: "cc"} {name: "new", city: "cc"} a and b are two addresses in memory, but the values are the same pointer to the same object in memory.Copy the code