Home >Web Front-end >Front-end Q&A >There are several types of JavaScript variables

There are several types of JavaScript variables

青灯夜游
青灯夜游Original
2021-07-20 16:18:137107browse

JavaScript variables have 9 types: 1. string type; 2. number type; 3. boolean type; 4. null type; 5. undefined type; 6. Object type; 7. Array type; 8. Function type; 9. Symbol type.

There are several types of JavaScript variables

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

What are the variable types in JavaScript?

  • Value type: string, number, boolean, null, undefined

  • ## Reference types: Object, Array, Function

  • ES6 introduces a new primitive data type: Symbol, which represents a unique value.


The difference between value type and reference type

Value type:

  • The occupied space is fixed and stored in the stack (when a method is executed, each method will establish its own memory stack, and the variables defined in this method will be put into this stack memory one by one. As When the execution of the method ends, the memory stack of this method will be naturally destroyed. Therefore, all variables defined in the method are placed in the stack memory; the stack stores basic variables and reference variables of some objects. Basic variables The value is stored on the stack, while the reference variable stored on the stack points to the address of an array or object in the heap. This is why modifying the reference type will always affect other reference variables pointing to this address.

  • What is saved and copied is the value itself

  • Use typeof to detect the type of data

  • Basic type data is a value type

Reference type:

  • The occupied space is not fixed and is saved in the heap (when we create a When an object is created, the object will be saved to the runtime data area for repeated use (because the creation cost of the object is usually large). This runtime data area is the heap memory. The objects in the heap memory will not be deleted when the method ends. Destroyed, even after the method ends, the object may be referenced by another reference variable (very common when passing method parameters), the object will still not be destroyed. Only when an object does not have any reference variables referring to it, the system The garbage collection mechanism will recycle it during verification.

  • What is saved and copied is a pointer to the object

  • Use instanceof to detect Data type

  • The object constructed using the new() method is a reference type

  • // 值类型:Number、string、bollean、undefined
    var a = 100
    var b = a
    a = 200
    console.log(b) // 100
     
     
    // 引用类型:对象、数组、函数、null(空指针)
    // 可以扩展属性
    var a = {age:20}
    var b = a
    b.age = 21
    console.log(a.age) // 21
     
    typeof undefined // undefined
    typeof 'abc' // string
    typeof 123 // number
    typeof true // boolean
    // typeof 区分不出来引用类型(除了函数)
    typeof {} // object
    typeof [] // object
    typeof null // object
    typeof console.log //function
[Recommended learning:

javascript advanced tutorial

The above is the detailed content of There are several types of JavaScript variables. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn