How do you check if a variable is an array in JavaScript? [duplicate]
https://stackoverflow.com/questions/767486/how-do-you-check-if-a-variable-is-an-array-in-javascript
|
1137down voteaccepted
|
There are several ways of checking if an variable is an array or not. The best solution is the one you have chosen.
This is the fastest method on Chrome, and most likely all other browsers. All arrays are objects, so checking the constructor property is a fast process for JavaScript engines. If you are having issues with finding out if an objects property is an array, you must first check if the property is there.
Some other ways are:
This method runs about 1/3 the speed as the first example. Still pretty solid, looks cleaner, if you're all about pretty code and not so much on performance. Note that checking for numbers does not work as
This last one is, in my opinion the ugliest, and it is one of the slowest. Running about 1/5 the speed as the first example. Array.prototype, is actually an array. you can read more about it here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray So yet another update
This guy is the slowest for trying to check for an Array. However, this is a one stop shop for any type you're looking for. However, since you're looking for an array, just use the fastest method above. Also, I ran some test: http://jsperf.com/instanceof-array-vs-array-isarray/33 So have some fun and check it out. Note: @EscapeNetscape has created another test as jsperf.com is down. http://jsben.ch/#/QgYAV I wanted to make sure the original link stay for whenever jsperf comes back online. |
How do you check if a variable is an array in JavaScript? [duplicate]的更多相关文章
- myeclipse使用maven插件进行maven install时报错check $m2_home environment variable and mvn script match
check $m2_home environment variable and mvn script match 分类: maven2015-09-01 18:06 842人阅读 评论(0) 收藏 举 ...
- -Dmaven.multiModuleProjectDirectory system propery is not set. Check $M2_HOME environment variable and mvn script match.
一, eclipse中使用maven插件的时候,运行run as maven build的时候报错 -Dmaven.multiModuleProjectDirectory system propery ...
- idea 14运行java工程报错-Dmaven.multiModuleProjectDirectory system propery is not set. Check $M2_HOME environment variable and mvn script match.
报错信息:Disconnected from the target VM, address: '127.0.0.1:59770', transport: 'socket' -Dmaven.multiM ...
- 接上一篇博客(解决-Dmaven.multiModuleProjectDirectory system property is not set. Check $M2_HOME environment variable and mvn script match. )
解决-Dmaven.multiModuleProjectDirectory system property is not set. Check $M2_HOME environment variabl ...
- maven 运行tomcatrun -Dmaven.multiModuleProjectDirectory system property is not set. Check $M2_HOME environment variable and mvn script match.
解决-Dmaven.multiModuleProjectDirectory system property is not set. Check $M2_HOME environment varia ...
- Intellij +Maven 报错: Dmaven.multiModuleProjectDirectory system property is not set. Check $M2_HOME environment variable and mvn script match.
在intellij使用 Maven Project 测试时,运行test时看到log里的报错信息: -Dmaven.multiModuleProjectDirectory system propert ...
- eclipse+Maven插件报错:-Dmaven.multiModuleProjectDirectory system propery is not set. Check $M2_HOME environment variable and mvn script match.
问题描述: eclipse indigo+maven3.3.3+jdk1.70 maven插件执行报错:-Dmaven.multiModuleProjectDirectory system prope ...
- -Dmaven.multiModuleProjectDirectory system propery is not set. Check $M2_HOME environment variable a
eclipse中使用maven插件的时候,运行run as maven build的时候报错 -Dmaven.multiModuleProjectDirectory system propery is ...
- -Dmaven.multiModuleProjectDirectory system property is not set. Check $M2_HOME environment variable and mvn script match.
在执行[maven clean]的时候报错,错误信息如下: -Dmaven.multiModuleProjectDirectory system property is not set. Check ...
随机推荐
- redis.conf介绍
默认配置文件: # Redis configuration file example. # # Note that in order to read the configuration file, R ...
- QT_5_ Qt中信号和槽 + 自定义信号和槽 + lambda 表达式
1.Qt中信号和槽 1.1 需求:点击按钮关闭窗口 1.2 利用connect进行链接 1.3 参数1 信号发送者(指针) 参数2 发送的信号(信号地址) 参数3 信号的接受者(指针) 参数4 处理槽 ...
- Mac 下用homebrew安装配置MongoDB
---恢复内容开始--- 1.首先安装homebrew,已有就跳过 /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent. ...
- 万能的搜索--之DFS(二)
(一)深度优先搜索(DFS) 我们先给出深度优先的解决办法,所谓深度优先搜索,在迷宫问题里就是不撞南墙不回头,能走得深一点就尽量深一点.如果碰到了墙壁就返回前一个位置尝试其他的方向.在<啊哈!算 ...
- UVa-133-救济金发放
这题的话,我们首先对于移动函数可以知道,因为只是顺逆的关系,也就是加一或者减一,所以我们每次移动的时候,都补上一个小于n的最大整数,然后取模,这样就不会有负数,而且加之后的结果不会超过2*n,所以我们 ...
- POJ-1724 深搜剪枝
这道题目如果数据很小的话.我们通过这个dfs就可以完成深搜: void dfs(int s) { if (s==N) { minLen=min(minLen,totalLen); return ; } ...
- Python的Turtle绘制纳兹咩的娘口三三
今天看完夏目友人帐的大电影,哭成了泪猴~ 所以我打算用Python画一只娘口三三陪伴在我身边 不过.. 画的太丑,还没上色..,你们可以完善一下~ 代码放在这里了 import turtle as t ...
- LeetCode(10) Regular Expression Matching
题目 Implement regular expression matching with support for '.' and '*'. '.' Matches any single charac ...
- php添加了环境变更,还是显示 不是内部或外部命令 (注:添加到目录即可,不加 php.exe )
重新配置了PHP环境,要安全PEAR扩展,CMD窗口运行PHP,提示不是内部或者外部命令或者可执行文件,解决方法是把PHP目录加入系统环境变量,不然的话,你只能CD到PHP安装目录下来运行PHP命令.
- STM32F407 串口通信实验 视频第27节 个人笔记
前言 第26节也是串口,笔记链接在此:https://www.cnblogs.com/YuQiao0303/p/10019362.html github地址:https://github.com/Yu ...