Js basic syntax
If a variable is declared with var and no value is assigned, the value of the variable is undefined
JS is a dynamically typed language, there is no limit to the type of a variable, you can change the type at any time
For two declarations of the same variable, the second declaration is invalid, but assignment overrides the previous one
The way JS works is to parse the code, get all declared variables, and then run it, so that all variable declaration statements are promoted to the code header, called “variable promotion”, while assignment does not
The tag can be used to break multiple loops with code blocks break and continue
Js data type
Typeof a data type that can return a value, returning “undefined” for an undeclared variable.
Numerical dependent global methods
PareseInt () can convert a string to an integer, with Spaces removed at the head, and as many strings as possible to integers. If the first character cannot be converted, the string is stopped. If the first character cannot be converted, NaN is returned
If the value starts with 0x 0x, it is parsed in hexadecimal format
For numbers that are automatically converted to scientific notation, parseInt() treats the scientific notation representation as a string
PareseInt () can accept a second constant indicating which base to convert to
Function declarations in block-level scopes are similar to lets, that is, they cannot be used externally. When declared inside a block, use function expressions as much as possible
Does it get assigned even though it’s impossible to run to,
The top-level object is Windows in the browser, global in Node, and the var function defines the object as a property of the top-level object, which is not defined in any other way
The const command indicates that the variable is a read-only variable that cannot be modified. Like let, it is only valid in the block-level scope and must be used after the declaration. Const guarantees that the address to which the variable points is invariant, which is true for simple types.
Deconstruction assignment
Object destructuring assignment
1. Arrays are ordered. The values of variables are determined by their positions, but the attributes of objects are not ordered, so the attributes and variable names must be the same
2. The function is to assign existing object methods to new variables
3.let {foo:baz} = {foo:'aaa',bar:'bbb'};
baz //"aaa"
This means that the internal mechanism for deconstructing an object’s assignment is to find the property of the same name and then assign it
4. Object deconstruction can specify default values, as long as the object property value is strictly undefined
var {x=3}={}
x//3
var {x=3}={x:null}
x //null
5. Do not apply to defined variableslet x; {x}={x:1}
Need to be({x}={x:1})
6.The rule for deconstructing assignment is to turn the right-hand side of the equation into an object whenever it is not an object or array
Destruct assignment of function arguments
function add([x,y]){
returnx + y; } the add ([1, 2]); / / 3Copy the code
[[1, 2], [3, 4]]. The map (((a, b)) = > a + b); / / [3, 7]Copy the code
The operator
Non-equality operators:
1. String comparison, dictionary order comparison (PS: Chinese also has Unicod code points)
2. Non-string comparison:
1 The primitive type is converted to a numeric comparison, and all NaN comparisons return false *
The valusOf method is called first, and the toString method is called if the object is returned
Equality operator
The equality operator compares equality, whereas strict equality compares whether two are “the same value.”
Strictly equal
Different types directly return false, while compound types compare whether to refer to the same address
JSON
Attribute names must be “” and numeric values are decimal only
JSON.stringify()
Json.stringify converts a value to a JSON string in a JSON format that can be restored by json.parse.
For strings of primitive type, the result is quoted.
example
JSON.stringify('foo') === "foo"
The result is “false” with an inner double quote to let the JS engine know that this is a string.
1. The object’s attribute is undefined, a function, or an XML object. This method is filtered by json.stringify.
2. Array members are undefined, functions, or XML objects, and these values are converted to NULL.
3. Regular objects are converted to empty objects.
4. The method can take a second argument specifying the property to be converted to a string.
var obj = {
'prop1': 'value1'.'prop2': 'value2'.'prop3': 'value3'
};
var sel = ['prop1'.'prop2'];
console.log(JSON.stringify(obj,sel));
Copy the code
String extension
Template string
1. Use backquotation marks (‘) as a regular string, define a multi-line string, or embed variables in a string.
Embed variables in strings
let name = "Bob",time = "today";
Hello ${name},how are you ${time}?
2. Use backquotes in template strings to escape with backslashes.
3. Use template strings to represent multi-line strings, with all whitespace and indentation retained in the output. This can be eliminated using the trim method.
4. Any JS expression can be placed in braces, operations can be performed, object attributes can be referenced, and functions can be called. If the value is not a string, toString is called.
5. “Tag template” : The template string can also be followed by the name of a function that will be called to process the template string.
let a = 5;
let b=10;
tagHello ${ a+b } world ${a*b}
;
Equivalent to tag([‘Hello ‘,’ world ‘,’],15,50);
The first argument to the tag function is an array, and the other arguments are the values to be substituted for the various variables in the template string.
Extension of a function
Default values for function arguments
For ES6, you can set default values for parameters
function log(x,y = 'world'){
console.log(x,y);
}
Copy the code