Home > Web Front-end > JS Tutorial > body text

Teach you a trick to determine whether JavaScript is an array

醉折花枝作酒筹
Release: 2021-08-10 13:38:59
Original
1403 people have browsed it

In the previous article we learned how to delete any element in an array, please see "How to delete any element in an array in javascript". This time we will learn about the method of determining whether an object is an array. You can refer to it if necessary.

We know how to create an array object, how to add elements to the array, and how to delete elements, but it seems that we have overlooked a serious problem, that is, how do we determine that this is an array? This time we will introduce the method of determining whether an object is an array.

First let’s look at a small example.

<script>
var arr = new Array(3); 
arr[0] = "one";
arr[1] = "two";
arr[2] = "three";
console.log(Array.isArray(arr));

console.log(Array.isArray([1, 2, 3]));

console.log(Array.isArray("1","2"));
</script>
Copy after login

The result of this example is

Teach you a trick to determine whether JavaScript is an array

We can see that the first one is true and the third one is false. According to past results, true definitely means "is an array", while false definitely means "not an array".

Let's take a look at the code. Why is the first result true, the second result also true, and the third result false? In fact, it can be clearly seen that the first one is judging an array, so the return value must be true, but the third one does not seem to be very different from the second one, so why is the second one an array? , and the third one is not an array? Let's take a look at this method in detail.

isArray() method is used to determine whether an object is an array.

Let’s take a look at the syntax of this method.

Array.isArray(要判断的对象)
Copy after login

The value returned by this method is a Boolean value. If the object is an array, it returns true, otherwise it returns false.

Let’s look back at this example. In this method, are the second and third the same? The difference is that after removing this method, the second one is actually "[1, 2, 3]", and the third one is ""1","2"", at first glance, you will know that the former one is an array, and the latter one can only be said to be a string.

I’ll stop here this time. If you need it, you can read: javascript advanced tutorial

The above is the detailed content of Teach you a trick to determine whether JavaScript is an array. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!