In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what are the ways to check whether the variable is an array in JS". In the daily operation, I believe many people have doubts about the way to check whether the variable is an array in JS. Xiaobian consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to you to answer the doubt that "check whether the variable is an array in JS". Next, please follow the editor to study!
The following code snippet is used to check whether a variable or value is an array. The Array.isArray method can be used in mainstream browsers. For older browsers, you can use polyfill
Const variable = ['?','?','?]; / / mainstream browser Array.isArray (variable); / / older browser Object.prototype.toString.call (variable) = ='[object Array]'
Modern methods of checking arrays
The best way to check an array is to use the built-in Array.isArray ()
Array.isArray ([]); / / true Array.isArray (['?]); / / true Array.isArray (new Array ('?)); / / true
Browser support
The browser supports Array.isArray () very well.
Polyfill for older browsers
You can use this MDN polyfill if you need support from older browsers.
If (! Array.isArray) {Array.isArray = function (org) {return Object.prototype.toString.call (org) = ='[object Array]'}}
Other ways: use Lodash or Underscore
If you are using external libraries, they also have some built-in methods.
Lodash
Check whether the value is an array object.
Const array = ['?','?','?]; const notArray = 'not array'; _ .isArray (array); / / true _ .isArray (notArray); / / false
Underscore
Returns true if the object is an array.
Const array = ['?','?','?]; const notArray = 'not array'; _ .isArray (array); / / true _ .isArray (notArray); / / false
Why can't we use typeof?
Usually, to check the type of value, we just need to use typeof
Typeof 'string'; / /' string' typeof 100; / / 'number' typeof true; / /' boolean' typeof false; / / 'boolean' typeof function () {}; / /' function' typeof {}; / / 'object' typeof []; / /' object'
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.