JavaScript language types:

  • undeifined
  • boolean
  • string
  • number
  • null
  • symbol
  • object
Remember: 0.1 + 0.2! = 0.3Copy the code
1.undefined
I pay, therefore, am a variable, not a keyword. To obtain its value, void 0 is used. Void 0 == undefined Any variable is undefined before assignment. Null is a defined but empty state.Copy the code
2.null
The null type has only one value, which is null, and its semantics represent null values. Null, unlike undefined, is a keyword, so you can safely use the null keyword to get a NULL value.Copy the code
3.Boolean
Boolean types have two values,trueandfalse. It's to determine what's true or false in a logical sense.Copy the code
4.String
String is used to represent text data. A string has a maximum length of 2^53-1. The meaning of string is not a string, but the UTF16 encoding of the stringCopy the code
5.number
Number is a floating point number, console.log(0.1+0.2 == 0.3); //falseIn fact. Floating-point operation characteristics, precision problemsCopy the code
6.symbol
Symbol, a new type introduced in ES6, is a collection of all non-string object keys. The entire object system in ES6 is reshaped by Symbol. Create: var mysymbol = symbol (using the global symbol function)"my symbol"Iterator -- Defines a default iterator for each object. This iterator can be used for... Iterator = new Object // Add iterator to Object o[symbol.iterator] =function() {var v = 0;return {
        next:function() {return {value: v++, done: v> 10}
        }
    }
}

for(var v of o)  console.log(v)

Copy the code
Object of 7.
Object is the most complex type and one of the core mechanisms of JS. An object is a collection of attributes, including data attributes and accessor attributes, both of which are key-value forms. Key can be a string or symbol typeCopy the code

Type conversion

StringToNumber

NumberToString

A boxing conversion converts a primitive type into its corresponding object. Example: The Symbol object cannot be called using new, but we can use the boxing mechanism to get a Symbol object. Use a function called call to force boxing. var symbolObject = (function () {
        return this;
    }).call(Symbol("a"))
    
    console.log(symbolObject instanceof Symbol); //trueConsole. log(typeof symbolObject) // object or use the object function to explicitly call the boxing ability in JavaScript code var symbolObject = object ((Symbol()'a'));
    console.log(Object.ptototype.toString.call(symbolObject)); // [Object Symbol]
Copy the code
Unboxing conversion: The ToPrimitive function converts an object type to a primitive type. The conversion of objects to strings and numbers follows the "unboxing before converting" principle. Var o = {valueOf: () => {console.log()"valueOf"); return {}}
        toString: () => { console.log("toString"); returnValueOf //2.toString //3.typeError o +""O [symbol.toprimitive] = () => {console.log(); //1. ToString //2.tvalueOf //3.typeError toPrimitive Symbol overwrites the original behavior o[symbol.toprimitive] = () => {console.log()"toPrimitive"); return "hello"}
    
    console,log(o + "")
Copy the code

Js conversion between strings and numbers

1. Conversion function

parseInt("10.5") // 10
parseInt("0xA") //10

parseFloat("1234blue") // 1234
parseFloat("blue") //NaN

Copy the code

2. Cast type

Boolean(value) -- Convert the given value to Boolean Number(value) -- Convert the given value to a Number(integer or floating-point Number) String(value) -- Convert the given value to a String Boolean(100) //true
Number(false) //0
Number(true/ / 1Copy the code

3. Use JS variable weak type conversion

var string = 1 + '12' // 112 string
string = string - 0 // 112 number
Copy the code