js data types and their judgment method examples

js data types and their judgment method examples

js data types

Basic data types: number, string, boolean, undefined, null, Symbol,

Reference data type: object

NaN belongs to number;
Function, Array, Date are all objects;

Basic data types except null can be judged by typeof, and reference data types except Function return Object

let a = 1,
 b = '2',
 c = true,
 d = undefined,
 e = null,
 f = Symbol('f'),
 g = function () {},
 h = [],
 i = new Date()
console.log(typeof a)
console.log(typeof b)
console.log(typeof c)
console.log(typeof d)
console.log(typeof e)
console.log(typeof f)
console.log(typeof g)
console.log(typeof h)
console.log(typeof i)

View the output

You can see that the typeof of null is object. This is a historical bug. If you are interested, you can refer to "The history of "typeof null""

You can use the following method to determine null

function checkNull(num) {
 return num === null
}

The detailed type of object can be determined by Object.prototype.toString.call()

function checkObject(obj) {
 return Object.prototype.toString.call(obj)
}
console.log(checkObject(g))
console.log(checkObject(h))
console.log(checkObject(i))

You can see the output results

It can also be judged by the constructor constructor()

console.log(g.constructor === Function)
console.log(h.constructor === Array)
console.log(i.constructor === Date)

You can see the output results

Summarize

This is the end of this article about js data types and their judgment methods. For more relevant js data types and judgment content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Summary of 4 methods for determining data types in js and jquery
  • 4 ways to determine data types in JavaScript
  • js data type judgment method
  • Examples of correct judgment methods for data types in JS
  • Four methods of using JS to determine data types
  • Four data type judgment methods in JS
  • Share several methods of JavaScript type judgment

<<:  MySQL and MySQL Workbench Installation Tutorial under Ubuntu

>>:  How to build php7 with docker custom image

Recommend

Implementation of IP address configuration in Centos7.5

1. Before configuring the IP address, first use i...

Record a pitfall of MySQL update statement update

background Recently, I executed a DML statement d...

How to change $ to # in Linux

In this system, the # sign represents the root us...

MySQL not null constraint case explanation

Table of contents Set a not null constraint when ...

How to install FastDFS in Docker

Pull the image docker pull season/fastdfs:1.2 Sta...

Methods and steps to access Baidu Maps API with JavaScript

Table of contents 1. Baidu Map API Access 2. Usin...

Summary of methods for querying MySQL user permissions

Introduce two methods to view MySQL user permissi...

MySQL optimization query_cache_limit parameter description

query_cache_limit query_cache_limit specifies the...

Use of MySQL SHOW STATUS statement

To do MySQL performance adjustment and service st...

What are the rules for context in JavaScript functions?

Table of contents 1. Rule 1: Object.Method() 1.1 ...

Detailed explanation of Getter usage in vuex

Preface Vuex allows us to define "getters&qu...

How to encapsulate query components based on element-ui step by step

Table of contents Function Basic query functions ...

Detailed explanation of GaussDB for MySQL performance optimization

Table of contents background Inspiration comes fr...