Introductory understanding of JS data types

1. Classification (2 categories)


  * Basic (value) type
    * Number: arbitrary numeric value
    * String: arbitrary text
    * Boolean: true/false
    * undefined: undefined
    * null: null
  * Object (reference) type
    * Object: arbitrary object
    * Array: Special object type (below (Standard/internal data in order)
    * Function: Special object type (executable)


2. Judgment


  * typeof:
    * Can distinguish: numeric value, string, boolean, undefined, function * Ca
    n’t distinguish: null and object, general object and array
  * instanceof
    * Specially used to determine the type of object data: Object, Array and Function
  * == =
    * Can be judged: undefined and null

// typeof: 返回的是数据类型的字符串表达形式
  //1. 基本类型
  var a
  console.log(a, typeof a, a===undefined,66666666666) // undefined 'undefined' true
  console.log(a==typeof a) // false
  //typeof 返回的字符串,a==typeof a 为false
	
  a = 3
  console.log(typeof a === 'number')
  a = 'atguigu'
  console.log(typeof a === 'string')
  a = true
  console.log(typeof a === 'boolean')
 
  a = null
  console.log(a===null) // true
  console.log(typeof a) // 'object'

  console.log('--------------------------------')
 
  //2. 对象类型
  var b1 = {
    b2: [2, 'abc', console.log],
    b3: function () {
      console.log('b3()')
    }
  }
  console.log(b1 instanceof Object, typeof b1) // true 'object'
  console.log(b1.b2 instanceof Array, typeof b1.b2) // true 'object'
  console.log(b1.b3 instanceof Function, typeof b1.b3) // true 'function'

  console.log(typeof b1.b2[2]) // 'function'
  console.log(b1.b2[2]('abc')) // 'abc' undefined
  
  
  var arr = [];
  console.log(arr instanceof Object);//true

 3. Small tips

1. The difference between undefined and null?
  * undefined means no assignment
  * null means assignment, but the value is null
2. When do you assign a variable to null?
  * var a = null //a will point to an object, but the object is this It has not yet been determined
  * a = null //Let the object pointed to by a become a garbage object
3. Strictly distinguish between variable type and data type?
  * js variable itself has no type, and the type of variable is actually the type of data in the variable memory
  * Variable type:
    * Basic type: Variable that saves basic type data
    * Reference type: Variable that saves object address value
  * Data object
    * Basic type
    * Object type

Guess you like

Origin blog.csdn.net/m0_43599959/article/details/109502664