The data type

This article focuses on data types in JavaScript


01. Data type

  • The whole can be divided into primitive types and reference types

    • Primitive types (or primitive data types) include seven types of data:Number,String,Boolean,Symbol,BigInt,Null,Undefined
    • The reference type is:Object
  • What is the difference between a primitive type and a reference type?

    • Basically, raw type data is kept in stack memory
    • Referential data is stored in the heap, and a pointer (address) is stored in the stack, which points to the data in the heap

    I’m not going to expand the stack here

(1) Number type

  • The Number type uses the IEEE 754 format to represent integers and floating point numbers

    • So when you do floating-point calculations, you get errors of accuracy
  • Represents the number:

    • In decimal notation, write:num = 10
    • Octal, the first digit must be0:num = 0123
    • Hexadecimal prefix0x:num = 0x1f
    • A floating-point number must contain a decimal point and be followed by a number:Num = 1.2
    • Scientific notation in which a number is followed by a lettereAnd then add a power of 10 to be multiplied:max = 3e10,Min = 3.1 e-10
  • Special:

    • In JS, the maximum value is number. MAX_VALUE, and the minimum value is number.min_value

    • If you go beyond this range, it will be converted to Infinity, -infinity

    • A special value that is Not a Number: NaN — Not a Number

      • NaNIt’s not equal to anything, including itself
      • You can useisNaN()Method to determine whether a value isNaN
    • There are two ways to represent 0: +0 and -0

     console.log(+0 === -0) // true
     console.log(1 / +0) // +Infinity
     console.log(1 / -0) // -Infinity
     console.log(0 / 0) // NaN
     console.log(NaN == NaN) // false
     console.log(isNaN(NaN)) // true
     ​
    Copy the code

(2) String type

  • The String type can be represented by double quotation marks (“”), single quotation marks (“”), and backquotation marks (“”)

     let str1 = "a string"
     let str2 = 'a new string'
     let str3 = `a string too`
     ​
    Copy the code
  • Strings are immutable. Once created, they cannot be modified. All changes to the string are destroyed and a new string is returned

(3) Boolean

  • A Boolean type is a logical entity with only two values:true | false

(4) Symbol type

  • The Symbol type can be used to represent a unique, unmodifiable value

    • Can be used to ensure that object attributes use unique identifiers and that no attribute conflicts occur
    • Symbol is not enumerable, only throughObject.getOwnPropertySymbols()
  • Usage :(use a variable to store the value of Symbol and then use the variable, usually as a key in an object)

    • useSymbol()The function is initialized
    const foo = Symbol(); Const bar = Symbol(); const bar = Symbol(); Console. log(foo == bar) // false, each Symbol is unique let obj = {} obj[foo] = 1 obj[bar] = 2 console.log(obj) // {Symbol(): 1, Symbol(): 2} // Symbol is not enumerable, Only through the Object. GetOwnPropertySymbols () Object. Keys (obj) / / [] Object. The values (obj) / / [] Object. GetOwnPropertyNames (obj) / / []Copy the code

(5) BigInt

  • The BigInt type can represent arbitrarily large integers that exceed the safe integer limit for numbers

  • Add an n to the numeric value, or use the BigInt() function

    • All operations on BigInt return BigInt
    • However, you cannot mix BigInt and Number
     let num1 = 123456789n
     let num2 = BigInt(123456789)
     console.log(num1 == num2) // true
    Copy the code

(6) Null type

  • The Null type has only one valuenullRepresents a null object pointer

(7) Undefined

  • Undefined has only one value, Undefined, representing a variable that has not been assigned (initialized)

    • The variable is defined without an assigned value, which defaults toundefined, but it is not explicitly assigned toundefined
    • The function returns no value, by defaultundefined

(8) Object type

  • The Object type is a set of key-value pairs

    • Key (key) : or attribute name, is a string, orSymbol
    • Value (value) : or attribute values, which can be any type of value
     let obj1 = {
         name: 'Ruovan',
         age: 24
     }
     ​
     let obj2 = new Object({
         name: 'Ruovan',
         age: 24
     })
    Copy the code

I front-end side dish chicken, if there is wrong, please forgive