This article belongs to the original article, reprint please note — fromTao Yuan xiao Pan’s blog

preface

Why I write this article is not because I don’t know these types, but because I often forget one or two of them. This kind of knowledge, in fact, is memory-based knowledge, does not require much in-depth understanding.

It’s not easy to remember so many things at once. It’s easy to remember things when you first learn them, and then forget them after a while because there’s no long-term memory. So I did the classification management. Every time I mentioned this problem, I first thought of what categories there were, and then recalled what contents there were under each category.

Variables in JavaScript do not have types, only values.

Seven basic types

  • number
  • string
  • boolean
  • null
  • undefined
  • symbol
  • bigint

Primitive types are also called simple data types or primitive types.

We split it into three types:

The first is the common type:

Almost all programming languages have these types

  • Digital number
  • String string
  • Boolean value Boolean

The second is the more specific type of JS:

  • Null null
  • Undefined undefined

The third type is the new type:

We remember it by the ES release

  • Symbol ES2015 New (ES6)
  • Added to bigInt ES2020

How to identify the seven basic types?

Take a look at this code:

Typeof 12 // number Typeof 'hello world' // string Typeof true // Boolean Typeof null // Object (special) typeof undefined // undefined typeof Symbol() // symbol typeof BigInt(1) // bigintCopy the code

Typeof is correct, only null is correct, this is a BUG in JS. We just need to remember the exception.

In JavaScript, the first three bits of a binary are all zeros, and the first three bits of a binary are all zeros. Therefore, when typeof is executed, “Object” is returned.

The compound condition determines the NULL type

var a = null (! a && typeof a === 'object') // trueCopy the code

Complex type Object

Together with the object, the first seven basic types are called the eight built-in types.

There are two commonly mentioned subtypes of object:

  • An array of array
  • Function is the function

There are many subtypes:

  • String
  • Number
  • Boolean
  • Date
  • RegExp
  • Error

How to determine function type

Typeof detects function types correctly

function foo () {}

typeof foo // function
Copy the code

How to determine the array type

This can be done in a variety of ways:

var arr = [1]

typeof arr                // object
arr instanceof Array      // true
arr.constructor === Array // true
Array.isArray(arr)        // true
Copy the code

Typeof cannot be used to detect arrays. Array.isArray is recommended.

Universal type judgment method

function test(a) {
	return Object.prototype.toString.call(a).slice(8, -1)
}


test(123)          // Number
test([1])          // Array
test(function(){}) // Function
Copy the code

conclusion

Maybe this way does not help you, then you should find their own way of learning.

Welcome to discuss and discuss learning methods.