javascript Array Definition and Array Length
- 2021-06-28 06:17:46
- OfStack
This article provides a simple js introductory tutorial, this is an js array definition and array length example tutorial, if you are learning js array, we here tell you how to define arrays and add arrays and array length calculation examples.
Let's first look at how to define an array
var a = new array();
var b = new array(8);
var c = new array("first", "second", "third");
Or array direct quantity:
var d = ["first", "second", "third"];
Let's look at 1 and add 1 element after the array
var myarray = [];
myarray[myarray.length] = 'new element';
Length of Array
array has only one attribute, which is
length
length represents the number of memory occupied by the array, not just the number of elements in the array. In the array just defined, the value of b. length is 8
<script>
var a = new Array("first", "second", "third")
a[48] = "12"
document.write(a.length)
// The result displayed is
49
</script>
Let's take a look at an example of modifying the length of an array
var myarray = [1,2,3];
myarray.length // The initial length is
3
myarray.length = 2; // Delete the last
1 Elements
myarray.length = 20 // Add
18 Elements into an array