Summary of Common Knowledge Points in JavaScript Interview Development

  • 2021-07-09 06:56:18
  • OfStack

No1. Syntax and Types
1. Declare definitions
Variable type: var, defining variables; let, defining block domain (scope) local variables; const, which defines read-only constants.
Variable format: Begins with a letter, an underscore "_" or $sign, and is case sensitive.
Variable assignment: Declared but unassigned variables are undefined when used, and undeclared variables will throw exceptions when used directly.
Unassigned variable for calculation: the result is NaN. For example:
var x, y = 1;
console. log (x + y); //The result is NaN because x is not assigned.
2. Scope
Variable scope: Before ES6, there was no block declaration field, and variables acted on function blocks or globally. For example, the following code enters an x of 5.


 if (true) {
var x = 5;
}
console.log(x); // 5 

ES6 Variable scope: ES6 supports block scope, but requires let to declare variables. The following code outputs the result and throws an exception.


i f (true) {
let y = 5;
}
console.log(y); // ReferenceError: y is not defined1234 

Variable float: In a method or global code, we do not throw an exception when we use a variable before the life variable, but return undefined. This is because javascript automatically floats the declaration of a variable to the front of the function or the global. Such as the following code:


 /**
*  Global variable floating up 
*/
console.log(x === undefined); // logs "true"
var x = 3;

/**
*  Method variable floating up 
*/
var myvar = "my value";
//  Print variable myvar The results are: undefined
(function() {
console.log(myvar); // undefined
var myvar = "local value";
})(); 
 The above code is equivalent to the following code: 
 /**
*  Global variable floating up 
*/
var x;
console.log(x === undefined); // logs "true"
x = 3;

/**
*  Method variable floating up 
*/
var myvar = "my value";
(function() {
var myvar;
console.log(myvar); // undefined
myvar = "local value";
})(); 

Global variable: In the page, the global object is window, so we can access the global variable through window. variable. For example:


 version = "1.0.0";
console.log(window.version); // Output 1.0.0 

No2. Data structures and types
1. Data types
Six basic types: Boolean (true or false), null (js is case sensitive, which is different from Null and NULL), undefined, Number, String and Symbol (only 1 and immutable)
1 object type: object.
object and function: Objects act as containers for values, and functions act as application procedures.
2. Data conversion
Function: Strings can be converted to numbers using parseInt and parseFloat methods.
parseInt: The function signature is parseInt (string, radix), and radix is a number of 2-36 representing the number cardinality, such as decimal or 106-ary. The return result is integer or NaN, for example, the following output result is 15.


parseInt("0xF", 16);
parseInt("F", 16);
parseInt("17", 8);
parseInt(021, 8);
parseInt("015", 10);
parseInt(15.99, 10);
arseInt("15,123", 10);
parseInt("FXX123", 16);
parseInt("1111", 2);
parseInt("15*3", 10);
parseInt("15e2", 10);
parseInt("15px", 10); 

parseFloat: The function signature is parseFloat (string), and the return result is either a number or NaN. For example:


parseFloat("3.14"); // Returns a number 
parseFloat("314e-2"); // Returns a number 
parseFloat("more non-digit characters"); // Return NaN 

3. Textualization of data types
Text types: Array, Boolean, Floating-point, integers, Object, RegExp, String.
Extra comma case in Array: ["Lion", "Angel"], length 3, value of [1] undefiend. ['home', 'school',], the last comma is omitted so the length is 3. [, 'home', 'school'], length 4. ['home', 'school',], length 4.
integer Integer: Integers can be expressed as decimal, octal, 106-ary, binary. For example:


 0, 117 and -345 //10 Binary system 
015, 0001 and -0o77 //8 Binary system 
0x1123, 0x00111 and -0xF1A7 //106 Binary system 
0b11, 0b0011 and -0b11 1234 //2 Binary system  
 Floating point number: [(+|-)][digits][.digits][(E|e)[(+|-)]digits] . For example: 
 3.1415926 , -.123456789 , -3.1E+12 ( 3100000000000 ), .1e-23 ( 1e-24 )  

Object: Object's property get value can be obtained by ". property" or "[property name]". For example:


 var car = { manyCars: {a: "Saab", "b": "Jeep"}, 7: "Mazda" };
console.log(car.manyCars.b); // Jeep
console.log(car[7]); // Mazda 

Object properties: Property names can be arbitrary strings or empty strings. Invalid names can be enclosed in quotation marks. Complex names cannot be obtained by., but can be obtained by []. For example:


 var unusualPropertyNames = {
"": "An empty string",
"!": "Bang!"
}
console.log(unusualPropertyNames.""); // SyntaxError: Unexpected string
console.log(unusualPropertyNames[""]); // An empty string
console.log(unusualPropertyNames.!); // SyntaxError: Unexpected token !
console.log(unusualPropertyNames["!"]); // Bang! 

Conversion character: The following string output contains double quotation marks because the conversion symbol "\" is used.


 var quote = "He read \"The Cremation of Sam McGee\" by R.W. Service.";
console.log(quote);
// Output: He read "The Cremation of Sam McGee" by R.W. Service.1 .  

String wrap: Add "\" directly at the end of the string line, as shown in the following code:


i f (true) {
let y = 5;
}
console.log(y); // ReferenceError: y is not defined1234 
0

No3. Control Flow and Error Handling
1. Block expressions
Function: Block Expression 1 is used to control flow, such as if, for, while. In the following code {x + +;} Is a block declaration.


i f (true) {
let y = 5;
}
console.log(y); // ReferenceError: y is not defined1234 
1

Before ES6, there was no block scope: Before ES6, variables defined in block were actually contained in methods or globals, and the influence of variables was beyond the scope of block scope. For example, the following code ends up executing 2 because the variables declared in block act on the method.


i f (true) {
let y = 5;
}
console.log(y); // ReferenceError: y is not defined1234 
2

After ES6, there is a block domain scope: In ES6, we can change the block domain declaration var to let, so that the variable only scopes the block scope.

2. Logical judgment
Special values judged as false: false, undefined, null, 0, NaN, "".
Simple boolean and object Boolean types: false and true of simple boolean type are different from false and true of object Boolean type, and they are not equal. Such as the following example:


 var b = new Boolean(false);
if (b) //  Return true
if (b == true) //  Return false 

No4. Exception Handling
1. Type of exception
Throw exception syntax: Throw exception can be of any type. As shown below.


i f (true) {
let y = 5;
}
console.log(y); // ReferenceError: y is not defined1234 
4

Custom exception:


i f (true) {
let y = 5;
}
console.log(y); // ReferenceError: y is not defined1234 
5

2. Grammar
Keyword: Use try {} catch (e) {} finally {} syntax, similar to C # syntax.
finally return value: If an return statement is added to finaly, the return value is return of finally regardless of what the entire try. catch returns. As shown below:


i f (true) {
let y = 5;
}
console.log(y); // ReferenceError: y is not defined1234 
6

finally annexation anomaly: If there is an return in finally and an throw anomaly in catch. Exceptions for throw are not caught because they have been overwritten by return for finally. As shown in the following code:


i f (true) {
let y = 5;
}
console.log(y); // ReferenceError: y is not defined1234 
7

System Error objects: We can use Error {name, message} objects directly, for example: throw (new Error ('The message'));


Related articles: