Home > Web Front-end > JS Tutorial > How to use arrays in JavaScript

How to use arrays in JavaScript

不言
Release: 2018-12-21 13:23:33
Original
4163 people have browsed it

Arrays in How to use arrays in How to use arrays in How to use arrays in JavaScript are often used, so how to use How to use arrays in How to use arrays in How to use arrays in JavaScript arrays specifically? This article will share with you the usage of arrays in How to use arrays in How to use arrays in How to use arrays in JavaScript.

How to use arrays in How to use arrays in How to use arrays in JavaScript

Without further ado, let’s look at specific examples~

In an array, multiple values ​​can be placed in one bracket.

At this time, put the three values ​​​​(elements) Tom, Jerry, and Holly in the brackets defined as "family".

Values ​​are enclosed in [].

var family = ["Tom", "Jerry", "Holly"];
Copy after login

Let’s output family

<script>  
var family = [&quot;Tom&quot;, &quot;Jerry&quot;, &quot;Holly&quot;];  
  console.log(family);
</script>
Copy after login

The running results are as follows

How to use arrays in How to use arrays in How to use arrays in JavaScript

As you can see from the running results, Tom , Jerry and Holly are all included in family.

Specify and get the array index (subscript)

Next, try to get each element individually by specifying the index.

In [], numbers (indexes) are assigned in the order 0,1,2,..., including 0.

So, for example, when you want to retrieve the value of Jerry, you can write like this.

<script>
  var family = [&quot;Tom&quot;, &quot;Jerry&quot;, &quot;Holly&quot;];
  console.log(family[1]);
</script>
Copy after login

The running effect is as follows

How to use arrays in How to use arrays in How to use arrays in JavaScript

Replace elements

I will replace Holly with Susan below , because Holly’s subscript (index) is 2, so we can do this

<script>
  var family = [&quot;Tom&quot;, &quot;Jerry&quot;, &quot;Holly&quot;];
  family[2]="Susan";
  console.log(family);
</script>
Copy after login

The running results are as follows

How to use arrays in JavaScript

Finally let’s take a look Attributes of arrays

Attributes mean "characteristics/characteristics".

Let's introduce an array attribute

< length >

The length attribute...can get the length of the array, that is, the number of elements.

Let’s take a look at the specific use

<script>
  var family = [&quot;Tom&quot;, &quot;Jerry&quot;, &quot;Holly&quot;];  
  console.log(family.length);
  </script>
Copy after login

The running results are as follows

How to use arrays in JavaScript

##This article ends here, about For other properties and usage of How to use arrays in How to use arrays in How to use arrays in JavaScript arrays, you can refer to the two articles

How to use Array objects in How to use arrays in How to use arrays in How to use arrays in JavaScript and What are the usages of Array objects in How to use arrays in How to use arrays in How to use arrays in JavaScript.

The above is the detailed content of How to use arrays in JavaScript. 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