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

How to set array length in javascript

醉折花枝作酒筹
Release: 2021-08-10 15:24:55
Original
5793 people have browsed it

In the previous article, we learned about the method of reversing the position of elements, please see "How to reverse the position of elements in javascript". This time we will learn about the method of setting the array length. You can refer to it if necessary.

We knew a lot about JavaScript arrays before, but we didn’t seem to have mentioned the length attribute in detail.

Let’s look at a small example first.

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

var nums=new Array(6);
nums[0] = "one";
nums[1] = "two";
nums[2] = "three";
console.log(nums.length);

var nums=new Array(2);
nums[0] = "one";
nums[1] = "two";
nums[2] = "three";
console.log(nums.length);
</script>
Copy after login

The result of this small example is

How to set array length in javascript

We can see that the result of this small example is3,6, 3, but is this because of the array length specified when we created it? Let’s take a look at the code. The length we specified for the first one is three, so the length of the array is three. There is no problem at all. But for the second one, the length specified is 6. The length of the array is 6. There is no problem at all. question? For this third one, the specified length is obviously 2. Why is the length of this array three?

With these questions, let’s take a look at the length attribute.

The length property sets or returns the number of elements in the array. We all know this, but there seems to be no way to explain it in this introduction. Don’t worry, let’s take a look again.

There is a close connection between the length property of a JavaScript array and its numeric subscript. When a legal subscript needs to be used to assign a value to an array element, and the subscript exceeds the size of the current array, the interpreter will modify the value of length at the same time.

So let’s look at this example. Although the second example does not define six elements, we have defined the length of the array, so the length of the array is the length we defined; let’s look at the third one. Although we have The length is defined, but obviously the length we defined cannot accommodate the elements we defined, so the interpreter directly modified the value of length for us.

That’s all. If you need it, you can read: javascript advanced tutorial

The above is the detailed content of How to set array length 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