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

Details on how to use class styles in Vue

Table of contents 1. Boolean 2. Expression 3. Mul...

A quick solution to the problem of PC and mobile adaptation

When making a web page, we usually need to consid...

MySQL limit performance analysis and optimization

1. Conclusion Syntax: limit offset, rows Conclusi...

MySQL query redundant indexes and unused index operations

MySQL 5.7 and above versions provide direct query...

How to migrate the data directory in mysql8.0.20

The default storage directory of mysql is /var/li...

Vue implements the right slide-out layer animation

This article example shares the specific code of ...

How to get the height of MySQL innodb B+tree

Preface The reason why MySQL's innodb engine ...

Detailed explanation of Vue filter implementation and application scenarios

1. Brief Introduction Vue.js allows you to define...

How to modify the group to which a user belongs in Linux

Modify the group to which a user belongs in Linux...

TCP third handshake data transmission process diagram

The process packets with the SYN flag in the RFC7...

Implementation of Nginx configuration of local image server

Table of contents 1. Introduction to Nginx 2. Ima...

Html/Css (the first must-read guide for beginners)

1. Understanding the meaning of web standards-Why...