1. Language Foundation

1, grammar,

  • Case sensitive

  • Identifier – Is the name of a variable, function, attribute, or function parameter. The first character of an identifier must be a letter, underscore (_), or dollar sign ($)

  • Note: // single-line comment /* multi-line comment */

  • Statements: JavaScript statements end with a semicolon. Omitting the semicolon means that the parser determines where the statement ends


2. Keywords and reserved words

Ecma-262 describes a set of reserved keywords that have special uses, such as indicating the beginning and end of a control statement, or performing a specific operation.

By rule, reserved keywords cannot be used as identifiers or attribute names.


3, variables,

ECMAScript variables are loosely typed and can be used to hold any type of data. Each variable is nothing more than a named placeholder to hold arbitrary values.

There are three keywords to declare variables: var, const, and let.

var

  • Scope: A declared scope is a function scope

  • Declaration promotion – variables declared by words are automatically promoted to the top of the function scope

let

  • Scope: Declares that the scope is a block-level scope

  • Invariant promotion

  • The use of let declarations in the for loop solves the problem of variables seeping outside the body of the loop caused by the previous use of var loops

const

Const behaves much the same as let, with the only important differences being that variables declared with it must also be initialized, and attempts to modify a variable declared by const result in a runtime error.

  • Scope: block-level scope
  • constIf a variable refers to an object, you can modify properties inside the object

Declare style and best practices

  • Do not use the var

    Limiting yourself to lets and const helps improve code quality because variables have clear scopes, declared locations, and unchanging values.

  • Const takes precedence over let

    Using const declarations allows the browser runtime to force variables to remain unchanged, and static code analysis tools to detect illegal assignments ahead of time. Using const declarations gives developers more confidence that certain variables will never change, and can quickly detect unexpected behavior caused by unexpected assignments.


4. Data types

ECMAScript has six simple data types (also known as primitive types) and one complex data type. You cannot define your own data types in ECMAScript, and all values can be represented by one of the seven data types mentioned above.

  • Simple data types: Undefined,Null,Boolean,Number,StringandSymbol.
  • Complex data types: An Object. Object is an unordered collection of name-value pairs.

typeofThe operator

The typeof operator is used to determine the data typeof any variable.

Using the Typeof operator on a value returns one of the following strings:

  • undefined“Indicates that the value is not defined;
  • boolean“Represents a Boolean value;
  • string“Indicates the value is a string;
  • number“Indicates that the value is numerical;
  • objectIndicates that the value is an object (rather than a function) or NULL;
  • function“Indicates that the value is a function;
  • symbol“Indicates that the value is a sign

Undefinedtype

Undefined has only one value, which is the special value Undefined. When a variable is declared using var or let but not initialized, it is equivalent to assigning undefined value to the variable:

Null type

The Null type also has only one value, the special value Null. Logically, a null value represents an empty object pointer, which is why passing a NULL to typeof returns Object.

When defining variables that will hold object values in the future, it is recommended to use NULL for initialization and not other values. That way, you can check for null to see if the variable was later reassigned a reference to an object,

Boolean type

The Boolean type is one of the most frequently used types in ECMAScript and has two literals: true and false.

Although there are only two Booleans, all other ECMAScript type values have their corresponding Boolean equivalents:

It is important to understand the above conversions because flow control statements like if automatically perform conversions from other types of values to Booleans:

let message = "Hello world!";

if (message) {
console.log("Value is true");
}
// Value is true
Copy the code

Numbertype

The Number type uses IEEE 754 format to represent integer and floating point values (also called double values in some languages).

1) NaN

A special value called NaN, which means “not a value,” is used to indicate that an operation intended to return a value failed (as opposed to throwing an error)

The isNaN() function, which takes an argument of any data type and converts it. Any value that cannot be converted to a numeric value causes the function to return true.

2) Numerical conversion

  • Number()Transformation function, can be used forAny data type.
  • parseInt()andparseFloat()The two functions are mainly used forConverts a string to a value.

ParseInt () and parseFloat() are recommended when using Number().

Type String

The String data type represents a sequence of zero or more 16-bit Unicode characters. Strings can be marked with double quotation marks (“), single quotation marks (‘), or backquotation marks (‘).

Template literals

  • ES6 added template literals using back quotes (‘).

    • String differentials, instead of using single quotes and + concatenation earlier, template strings can use include${expression}Represents a placeholder.
    const people = {
      name: 'Johnson'.job: 'Front-end'
    }
    
    const project = {
      name: 'Dyniva'
    }
    // The previous stitching
    let message = people.name + '(' + people.job + ') ' + ' follow this project ' + project.name;
    //ES6 template literal
    let message = `${people.name}(${people.job}) follow this project ${project.name}`
    Copy the code
  • All inserted values are coerced into strings using toString(), and any JavaScript expression can be used for interpolation.

  • Functions and methods can be called in interpolation expressions:

  • function capitalize(word) {
     return `${ word[0].toUpperCase() }${ word.slice(1)}`;
    }
    console.log(`${ capitalize('hello')}.${ capitalize('world')}! `); // Hello, World!
    Copy the code
  • Template literals can hold newline characters and can define strings across lines:

Symbol type

The Object type

An object in ECMAScript is simply a collection of data and functionality.

Like java.lang.Object in Java, Object in ECMAScript is the base class from which other objects are derived. All properties and methods of type Object also exist on derived objects.

Each Object instance has the following properties and methods.

  • Constructor: The function used to create the current object. In the previous example, the value of this property was the Object() function.

  • HasOwnProperty (propertyName) : Used to determine whether a given property exists on the current object instance (not the prototype).

  • IsPrototypeOf (object) : Used to determine whether the current object is the prototype of another object.

  • PropertyIsEnumerable (propertyName) : Used to determine if a given property can be enumerated in a for-in statement. As with hasOwnProperty(), the property name must be a string.

  • ToLocaleString () : Returns a string representation of an object that reflects its localized execution environment.

  • ToString () : Returns a string representation of an object.

  • ValueOf () : Returns the string, numeric, or Boolean representation of an object. Usually the same as the return value of toString().

The operator

  • Unary operators:

    • Increment/decrement operators (++ I — I), unary addition, subtraction, multiplication and division operators, bitwise operators,

    • Boolean operators (logical non!, logic and&&, and logic or||)

    • Relational operators (less than, greater than, less than or equal to, and greater than or equal to),

    • Equality operator

      • Is equal to the(= =) andIs not equal to(! =) : Both operators proceed firstType conversion(often called a cast) to determine whether the operands are equal.
      • Are congruent( = = =) andNot congruent ( ! = =) : Only two operands are inDon’t convertAnd determine if it is equal.

Here are some special cases and comparison results:

  • The conditional operator is one of the most widely used operators in ECMAScript and has the same syntax as in Java

    variable = boolean_expression ? true_value : false_value;
    Copy the code
  • The comma operator can be used to perform multiple operations in a single statement

5, statements,

IF statement

The if statement is one of the most frequently used statements and has the following syntax:

if (condition) statement1 else statement2
Copy the code

The condition can be any expression, and the evaluation result is not necessarily a Boolean value. ECMAScript automatically calls the Boolean() function to convert the expression’s value to a Boolean value.

Do – while statement

While statement

A while statement is a test-loop statement that checks the exit condition before executing the code inside the loop.

while(expression) statement
Copy the code

For statement

The for statement is also a test-first statement, but adds initialization code before entering the loop and expressions to execute after the loop executes

for (initialization; expression; post-loop-expression) statement
Copy the code

The for in statement

A for-in statement is a strictly iterative statement that enumerates non-symbolic key attributes in an object

For-in always gets the key of an object or the index of an array or string.

for (property in expression) statement
// Loop displays all attributes of the BOM object window
let arr = ['d'.'s'.'r'.'c'];
for (const key in arr) {// To ensure that this local variable is not modified, const is recommended
    if (arr.hasOwnProperty(key)) {
        console.log(key); }}/ / 0
/ / 1
/ / 2
/ / 3
Copy the code

The for – statement

A for-of statement is a strict iteration statement that iterates over the elements of an iterable *

For-of always gets the value of an object or the value of an array or string

/ / grammar
for (property of expression) statement
/ / sample
for (const el of ['d'.'s'.'r'.'c']) { // To ensure that this local variable is not modified, const is recommended
 console.log(el);
}
// d
// s
// r
// c
Copy the code

Label statement

Break and continue statements

Break is used to exit the loop immediately, forcing the next statement after the loop. The continue statement is also used to exit the loop immediately, but is executed again from the top of the loop.

A switch statement

Grammar:

switch (expression) {
 case value1:
 statement
 break;
 / /...
 default:
 statement
}
Copy the code

6, functions,

Functions in ECMAScript are declared using the function keyword, followed by a set of parameters, followed by the function body.

function functionName(arg0, arg1,... ,argN) {
 statements
}
Copy the code