This is the fifth day of my participation in the August Genwen Challenge.More challenges in August

The data type

ECMAScript has six simple data types: Number, String, Boolean, Null, and Undefined Symbol. Symbol is newly added to ES6

Type String

1. character literals

String types can be represented by single (‘), double (“), or backquotes (‘). And you can’t mix them. The string type contains some character literals.

literal meaning literal meaning
\n A newline \t TAB
\b backspace \r enter
\f Change the page \ Backslash (\)
\ ‘ Single quotes \” Double quotation marks
\xnn Hexadecimal encoding The character represented by NN \unnn Hexadecimal code NNNN indicates the character

2. Character string features

Once a string is created, its value does not change. If you change the value of a string, you essentially destroy the original string and store another string entry containing the new value into the variable.

3. Convert strings

The first is the most common method, toString(), whose sole purpose is to return the string equivalent of the current value. The toString() method can be used for numeric values, booleans, objects, and strings themselves. Null and undefined have no toString() method. The toSring() method can take a base argument that controls the string representation of the output value.

    let num = 10;
    congsole.log(num.toString());   / / 10
    congsole.log(num.toString(2));  / / 1010
    congsole.log(num.toString(8));  / / 12
    congsole.log(num.toString(10)); / / 10
    congsole.log(num.toString(16)); //a
Copy the code

If you are not sure whether a value can be used directly with the toString() method, you can use the String() function, which returns a String representing the value of the corresponding type. If the value has a toString() method, the toString() method is called and returns the result; If the value is null, return “null”, if undefined, return “undefined”.

4. Template literals

Template literals, introduced in ES6, feature the ability to preserve newline characters and define strings across lines. The most common scenario for template literals is defining templates.

    let pageHTML = ` 
      

juejin

`
; Copy the code

5. String interpolation

The most common feature of template literals is string interpolation, where one or more values can be inserted into a contiguous definition. All inserted values are cast to strings using toString(). And any JS expression can be used for interpolation.

    let a = 5;
    let result = a + "Times" + a + "Equal" + ( a * a );// Common splice
    let nowresult = `${a}Multiplied by the${a}Is equal to the${ a * a }`;// Template string concatenation
Copy the code

6. Label functions

Template literals also support defining tag functions that allow you to customize the interpolation behavior. The label function takes the template separated by the interpolation notation and evaluates each expression.

7. Raw string

Using template literals allows you to retrieve the original template literal content directly, rather than the converted character representation, using the default String.raw tag function

    console.log(` \ \ `); Print escaped \console.log(String.raw` \ \ `); Print the original \\Copy the code

Symbol type

Symbol is the new data type in ES6. Symbols are primitive values, and symbol instances are unique and immutable. The purpose of the symbol is to ensure that object attributes are uniquely identified without the risk of attribute collisions. Using symbols is initialized with the Symbol() function.

    let sym = Symbol(a);Copy the code

The Object type

ECMAScript Object is the base class for all objects, and all objects have the following properties and methods:

  • Constructor: Holds the function used to create the current object.

  • HasOwnProperty: Checks whether a given property exists in the current object instance. The attribute name of the parameter must be specified as a string.

  • IsPrototypeOf: Used to check whether the incoming object is a prototype of the current object.

  • PropertyIsEnumerable: Checks if a given property can be enumerable using a for-in statement. The attribute name of the parameter must be specified as a string.

  • ToLocaleString () : Returns a string of objects corresponding to the locale of the execution environment.

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

  • ValueOf () : Returns string, number, and Boolean representations of objects. Usually the same as the return value of the toString() method.