Summary of common knowledge points for JavaScript interview development, and javascript knowledge points

Source: Internet
Author: User

Summary of common knowledge points for JavaScript interview development, and javascript knowledge points

No1. syntax and type
1. Declaration definition
Variable type: var, defines the variable; let, defines the local variable of the block domain (scope); const, defines the read-only constant.
Variable format: it must start with a letter, underscore "_", or $. It is case sensitive.
Variable Value assignment: When a declared but unassigned variable is used, the value is undefined. If an undeclared variable is used directly, an exception is thrown.
If no value is assigned to the variable, the result is NaN. For example:
Var x, y = 1;
Console. log (x + y); // The result is NaN, because x is not assigned a value.
2. Scope
Variable scope: no block declaration domain exists before ES6, and the variable acts on the function block or global. The following code input x is 5.

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

ES6 variable scope: ES6 supports block scope, but you need to use let to declare variables. The output result of the following code throws an exception.

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

Variable floating: In a method or global code, we do not throw an exception when using the variable before the life variable, but return undefined. This is because javascript automatically promotes the declaration of variables to the very beginning of the function or global. The following code:

/*** Global variable floating */console. log (x = undefined); // logs "true" var x = 3;/*** method variable floating */var myvar = "my value "; // print the variable myvar and the result is: undefined (function () {console. log (myvar); // undefinedvar myvar = "local value" ;}) (); The above code is equivalent to the following code: /*** global variable floating */var x; console. log (x = undefined); // logs "true" x = 3;/*** method variable floating */var myvar = "my value"; (function () {var myvar; console. log (myvar); // undefinedmyvar = "local value ";})();

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

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

No2. data structure and type 
1. Data Type 
Six basic types: Boolean (true or false) and null (JavaScript is case sensitive, which is different from Null and NULL), undefined, Number, String, Symbol (unique and immutable mark)
An object type: object.
Object and function: the object is the container of the value, and the function is the process of the application.
2. Data Conversion 
Function: You can use the parseInt and parseFloat methods to convert a string to a number.
ParseInt: The function signature is parseInt (string, radix). radix is a number ranging from 2 to 36 to represent the base number, for example, decimal or hexadecimal. The returned result is integer or NaN. For example, the output result below 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 a number or NaN. For example:

ParseFloat ("3.14"); // returns the number parseFloat ("314e-2"); // returns the number parseFloat ("more non-digit characters"); // returns NaN

3. Regionalization of Data Types
Regionalization type: Array, Boolean, Floating-point, integers, Object, RegExp, String.
The extra comma in Array: ["Lion", "Angel"], the length is 3, and the value of [1] is undefiend. ['Home', 'school ',], the last comma is omitted, so the length is 3. [, 'Home', 'school '], with a length of 4. ['Home', 'school ',], with a length of 4.
Integer: an integer can be expressed in decimal, octal, hexadecimal, or binary. For example:

0,117 and-345 // decimal 015,000 1 and-0o77 // octal 0x1123, 0x00111 and-0xF1A7 // hexadecimal 0b11, 0b0011 and-0b11 1234 // binary floating point: [(+ |-)] [digits] [. digits] [(E | e) [(+ |-)] digits]. Example: 3.1415926,-. 123456789,-3.1E + 12 (3100000000000),. 1e-23 (1e-24)

Object: You can obtain the property value of an object through ". Property" or "[attribute name. For example:

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

Object Property: The property name can be any string or a Null String. invalid names can be enclosed by quotation marks. Complex names cannot be obtained through., but can be obtained through. For example:

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

Conversion character: the output result of the following string 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 line feed: Add "\" directly at the end of the string line, as shown in the following code:

 var str = "this string \is broken \across multiple\lines."console.log(str); // this string is broken across multiplelines. 

No3. control flow and error handling
1. Block expression
Purpose: block expressions are generally used for control flow, such as if, for, and while. In the following code, {x ++;} is a block declaration.

 while (x < 10) {x++;} 

ES6 has no block domain scope before: Before ES6, variables defined in the block are actually included in the method or global, and the impact of variables is beyond the block scope. For example, the final execution result of the following code is 2, because the variables declared in the block act on the method.

 var x = 1;{var x = 2;}console.log(x); // outputs 2 

There is a block domain range after ES6: In ES6, we can change the block domain declaration var to let so that the variable only applies to the block range.

2. logical judgment
Special values that are judged to be false: false, undefined, null, 0, NaN, and ,"".
Simple boolean and object Boolean types: the false and true values of the simple boolean type are different from those of the Boolean Type of the object. The two values are not equal. Example:

Var B = new Boolean (false); if (B) // returns trueif (B = true) // returns false

No4. Exception Handling
1. Exception type
Throw exception Syntax: throwing an exception can be of any type. As shown below.

Throw "Error2"; // string type throw 42; // numeric type throw true; // Boolean Type throw {toString: function () {return "I'm an object! ";}}; // Object type

Custom exception:

// Create an object type UserExceptionfunction UserException (message) {this. message = message; this. name = "UserException";} // rewrite the toString method to directly obtain useful information when an exception is thrown. prototype. toString = function () {return this. name + ': "' + this. message + '"';} // create an object and throw it throw new UserException (" Value too high ");

2. Syntax
Keyword: Use the try {} catch (e) {} finally {} syntax, similar to the C # syntax.
Finally return value: if a return statement is added to finaly, the return value is finally return regardless of what is returned by try. catch. As follows:

Function f () {try {console. log (0); throw "bogus";} catch (e) {console. log (1); return true; // The return Statement is paused until the finally execution is complete. log (2); // unexecuted code} finally {console. log (3); return false; // overwrite try. the return value of catch is console. log (4); // code not executed} // "return false" is executed now console. log (5); // not reachable} f (); // outputs 0, 1, 3; returns false

Finally consortion exception: If finally has return and catch has throw exception. Throw exceptions are not captured because they are overwritten by finally return. The following code is used:

Function f () {try {throw "bogus";} catch (e) {console. log ('caught inner "bogus" '); throw e; // throw statement is paused until finally execution is complete} finally {return false; // overwrite try. the throw statement} // has executed "return false"} try {f ();} catch (e) {// won't be executed here, because the throw in catch has been overwritten by the return Statement in finally. log ('caught outer "bogus" ');} // output // caught inner "bogus"

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

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.