search
HomeWeb Front-endJS TutorialDetailed explanation of how to judge js data type and array method instance

Data type judgment typeof

typeof can solve the judgment of most data types. Its return value is a string that describes the type of the operand.


//判断变量num是不是一个数字类型
if(typeof num=='number') {
  return true;
}

Return results:


var a="hling"; console.log(a); //string
var a=1; console.log(a); //number
var a=false; console.log(a); //boolean
var a; console.log(typeof a); //undfined
var a = null; console.log(typeof a); //object
var a = document; console.log(typeof a); //object
var a = []; console.log(a); //object
var a = function(){}; console.log(typeof a) 
//function除了可以判断数据类型还可以判断function类型

Except for the four types string, number, boolean, and undefined In addition, null, object, and array return all object types! ! !

For function types, function is returned, such as typeof(Date), typeof(eval), etc.

js method to determine array type

1) instanceof

instanceof is used to determine an Whether the variable is an instance of an object is a ternary operator. This operator has something to do with object-oriented in JavaScript. To understand this, you must first understand object-oriented in JavaScript. Because this operator detects whether the prototype chain of the object points to the prototype object of the constructor.


a instanceof b?alert("true"):alert("false") 
//注意b值是你想要判断的那种数据类型,是不是一个字符串,比如Array

Example:


##

var arr = [1,2,3,1]; 
alert(arr instanceof Array); // true

2) constructor

Definition in W3C definition: The constructor attribute returns a reference to the array function that created this object


var arr = []; 
arr instanceof Array; // true 
arr.constructor == Array; //true

The method for judging various types is:


console.log("string".constructor == String);
console.log((123).constructor == Number);
console.log(false.constructor == Boolean);
console.log([].constructor == Array);
console.log({}.constructor == Object);

General method:


function isArray(object){
  return object && typeof object==='object' &&
      Array == object.constructor;
}

3) Characteristic judgment

object.isArray() to determine, the purpose is to accurately detect whether a value is an array. IE9+, Firefox 4+, Safari 5+, Opera 10.5+ and Chrome all implement this method. However, versions before IE8 are not supported.


function isArray(object){
  return object && typeof object==='object' &&  
      typeof object.length==='number' && 
      typeof object.splice==='function' &&  
       //判断length属性是否是可枚举的 对于数组 将得到false 
      !(object.propertyIsEnumerable('length'));
}

4) Object.prototype.toString.call


Object.prototype.toString.call(value) == '[object Array]'

The above is the detailed content of Detailed explanation of how to judge js data type and array method instance. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
es6数组怎么去掉重复并且重新排序es6数组怎么去掉重复并且重新排序May 05, 2022 pm 07:08 PM

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

JavaScript的Symbol类型、隐藏属性及全局注册表详解JavaScript的Symbol类型、隐藏属性及全局注册表详解Jun 02, 2022 am 11:50 AM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

原来利用纯CSS也能实现文字轮播与图片轮播!原来利用纯CSS也能实现文字轮播与图片轮播!Jun 10, 2022 pm 01:00 PM

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

JavaScript对象的构造函数和new操作符(实例详解)JavaScript对象的构造函数和new操作符(实例详解)May 10, 2022 pm 06:16 PM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

JavaScript面向对象详细解析之属性描述符JavaScript面向对象详细解析之属性描述符May 27, 2022 pm 05:29 PM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

javascript怎么移除元素点击事件javascript怎么移除元素点击事件Apr 11, 2022 pm 04:51 PM

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

整理总结JavaScript常见的BOM操作整理总结JavaScript常见的BOM操作Jun 01, 2022 am 11:43 AM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。

foreach是es6里的吗foreach是es6里的吗May 05, 2022 pm 05:59 PM

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.