A data type

Data type classification

Basic (value) types

  • String: Any character String
  • Number: any Number
  • Boolean: true/false
  • Undefined: Undefined
  • Null: Null

Object (reference) type

  • Object: any Object
  • Function: a special object (that can execute statements)
  • Array: a special object (numeric subscript, internal data ordered)

Data type judgment

typeof

  • Undefined number string Boolean function
  • Null and array cannot return object
  • Usage: console.log(typeof a); Return lowercase string

instanceof

  • You can determine the specific type of the object
  • Usage: console.log(a instanceof B); Return true/false

object

Type of object

function Person(name, age) { // constructor
    this.name = name
    this.age  = age 
}
Copy the code

Instance objects

var p = new Person('Tom'.18) // Create instance objects based on type
Copy the code

Strictly distinguish variable types from data types

Type of data

  • Basic types of
  • Object type

Type of variable

  • Basic type: Stores data of the basic type
  • Reference type: Holds the address value

thinking

Undefined and null?

  • Undefined: undefined
  • Null: Defines and assigns a value to null

When do I assign null to a variable?

  • Initial assignment: null: used for objects whose assignment is uncertain
  • End assignment to NULL: makes the object garbage so that it can be collected by the garbage collector