Home Web Front-end JS Tutorial Detailed explanation of several ways to detect data types in JavaScript

Detailed explanation of several ways to detect data types in JavaScript

Mar 23, 2017 pm 02:43 PM

In the process of programming with javaScript, we often encounter such a problem, that is, we need to detect the type of a data or variable, this article This article mainly introduces a summary of several ways to detect data type in javaScript. Those who are interested can learn more.

In the process of programming with javaScript, we often encounter such a problem, that is, we need to detect the type of a data or variable. So what methods are provided for us in javaScript? There are a lot of codes circulating on the Internet, but I found that some of them were wrong, so I simply used each method myself. Today I specially sorted it out for future reference.

1. Typeof detection

typeof is a unary operator, syntax: typeof (operand), the operand can be of any type. Its return value is a string that describes the type of the operand.

// var arr = { name:"john"}; // object
  // var arr = ["语文","数学"]; // object
  // function Person() {};  // typeof(Person) => function
  // var arr = '我是字符串' ; // string
  // var arr = 66 ;    // number
  // var arr = true ;   // boolean
  // var arr = new Person(); // object
  // var arr = undefined;  // undefined
  // var arr = null;   // object
  // var arr = /^\d{5,20}$/; // object
  // console.log( typeof(arr) );
Copy after login

2. instanceof detection

instanceof detects whether a object is an instance of another object , can be used in the inheritance relationship to determine whether an instance belongs to its parent type.

// var arr = '我是字符串' ;     // console.log( arr instanceof String ) => false
    // var arr = 66 ;         // console.log( arr instanceof Number ) =>false
    // var arr = true ;        // console.log( arr instanceof Boolean ) =>false
    // var arr = ["语文","数学"];   // console.log( arr instanceof Array ) =>true
    // var arr = { name:"john"};    // console.log( arr instanceof Object ) =>true
    // var arr = function Person(){}; //console.log(arr instanceof Function)=>true
    // var arr = undefined;      // console.log(arr instanceof Object)=>false
    // var arr = null;        // console.log(arr instanceof Object)=>false
    // var arr = /^\d{5,20}$/;    // console.log(arr instanceof RegExp)=>true
Copy after login

3. Object.prototype.toString.call detection

Use the native toString() method on Object.prototype to determine the data type. The usage method is as follows: Object.prototype.toString.call(value)

// var arr = '我是字符串' ;   //[object String]
    // var arr = 66 ;        //[object Number]
    // var arr = true ;       //[object Boolean]
    // var arr = ["语文","数学"];  //[object Array]
    // var arr = { name:"john"};  //[object Object]
    // var arr = function Person(){}; //[object Function]
    // var arr = undefined;      //[object Undefined]
    // var arr = null;         //[object Null]
    // var arr = /^\d{5,20}$/;     //[object RegExp]
    // console.log( Object.prototype.toString.call(arr) );
Copy after login

The above is the detailed content of Detailed explanation of several ways to detect data types in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What data type should be used for gender field in MySQL database? What data type should be used for gender field in MySQL database? Mar 14, 2024 pm 01:21 PM

In a MySQL database, gender fields can usually be stored using the ENUM type. ENUM is an enumeration type that allows us to select one as the value of a field from a set of predefined values. ENUM is a good choice when representing a fixed and limited option like gender. Let's look at a specific code example: Suppose we have a table called "users" that contains user information, including gender. Now we want to create a field for gender, we can design the table structure like this: CRE

MIT's latest masterpiece: using GPT-3.5 to solve the problem of time series anomaly detection MIT's latest masterpiece: using GPT-3.5 to solve the problem of time series anomaly detection Jun 08, 2024 pm 06:09 PM

Today I would like to introduce to you an article published by MIT last week, using GPT-3.5-turbo to solve the problem of time series anomaly detection, and initially verifying the effectiveness of LLM in time series anomaly detection. There is no finetune in the whole process, and GPT-3.5-turbo is used directly for anomaly detection. The core of this article is how to convert time series into input that can be recognized by GPT-3.5-turbo, and how to design prompts or pipelines to let LLM solve the anomaly detection task. Let me introduce this work to you in detail. Image paper title: Largelanguagemodelscanbezero-shotanomalydete

What is the best data type for gender fields in MySQL? What is the best data type for gender fields in MySQL? Mar 15, 2024 am 10:24 AM

In MySQL, the most suitable data type for gender fields is the ENUM enumeration type. The ENUM enumeration type is a data type that allows the definition of a set of possible values. The gender field is suitable for using the ENUM type because gender usually only has two values, namely male and female. Next, I will use specific code examples to show how to create a gender field in MySQL and use the ENUM enumeration type to store gender information. The following are the steps: First, create a table named users in MySQL, including

Improved detection algorithm: for target detection in high-resolution optical remote sensing images Improved detection algorithm: for target detection in high-resolution optical remote sensing images Jun 06, 2024 pm 12:33 PM

01 Outlook Summary Currently, it is difficult to achieve an appropriate balance between detection efficiency and detection results. We have developed an enhanced YOLOv5 algorithm for target detection in high-resolution optical remote sensing images, using multi-layer feature pyramids, multi-detection head strategies and hybrid attention modules to improve the effect of the target detection network in optical remote sensing images. According to the SIMD data set, the mAP of the new algorithm is 2.2% better than YOLOv5 and 8.48% better than YOLOX, achieving a better balance between detection results and speed. 02 Background & Motivation With the rapid development of remote sensing technology, high-resolution optical remote sensing images have been used to describe many objects on the earth’s surface, including aircraft, cars, buildings, etc. Object detection in the interpretation of remote sensing images

Mind map of Python syntax: in-depth understanding of code structure Mind map of Python syntax: in-depth understanding of code structure Feb 21, 2024 am 09:00 AM

Python is widely used in a wide range of fields with its simple and easy-to-read syntax. It is crucial to master the basic structure of Python syntax, both to improve programming efficiency and to gain a deep understanding of how the code works. To this end, this article provides a comprehensive mind map detailing various aspects of Python syntax. Variables and Data Types Variables are containers used to store data in Python. The mind map shows common Python data types, including integers, floating point numbers, strings, Boolean values, and lists. Each data type has its own characteristics and operation methods. Operators Operators are used to perform various operations on data types. The mind map covers the different operator types in Python, such as arithmetic operators, ratio

Add SOTA in real time and skyrocket! FastOcc: Faster inference and deployment-friendly Occ algorithm is here! Add SOTA in real time and skyrocket! FastOcc: Faster inference and deployment-friendly Occ algorithm is here! Mar 14, 2024 pm 11:50 PM

Written above & The author’s personal understanding is that in the autonomous driving system, the perception task is a crucial component of the entire autonomous driving system. The main goal of the perception task is to enable autonomous vehicles to understand and perceive surrounding environmental elements, such as vehicles driving on the road, pedestrians on the roadside, obstacles encountered during driving, traffic signs on the road, etc., thereby helping downstream modules Make correct and reasonable decisions and actions. A vehicle with self-driving capabilities is usually equipped with different types of information collection sensors, such as surround-view camera sensors, lidar sensors, millimeter-wave radar sensors, etc., to ensure that the self-driving vehicle can accurately perceive and understand surrounding environment elements. , enabling autonomous vehicles to make correct decisions during autonomous driving. Head

Detailed explanation of how to use Boolean type in MySQL Detailed explanation of how to use Boolean type in MySQL Mar 15, 2024 am 11:45 AM

Detailed explanation of how to use Boolean types in MySQL MySQL is a commonly used relational database management system. In practical applications, it is often necessary to use Boolean types to represent logical true and false values. There are two representation methods of Boolean type in MySQL: TINYINT(1) and BOOL. This article will introduce in detail the use of Boolean types in MySQL, including the definition, assignment, query and modification of Boolean types, and explain it with specific code examples. 1. The Boolean type is defined in MySQL and can be

What is the best data type choice for gender field in MySQL? What is the best data type choice for gender field in MySQL? Mar 14, 2024 pm 01:24 PM

When designing database tables, choosing the appropriate data type is very important for performance optimization and data storage efficiency. In the MySQL database, there is really no so-called best choice for the data type to store the gender field, because the gender field generally only has two values: male or female. But for efficiency and space saving, we can choose a suitable data type to store the gender field. In MySQL, the most commonly used data type to store gender fields is the enumeration type. An enumeration type is a data type that can limit the value of a field to a limited set.

See all articles