The length property provides the count of elements contained within an array, represented as a 32-bit unsigned integer. In other words, it yields a numerical value that indicates how many elements exist in the array. It is important to note that this return value is consistently greater than the largest array index.
The length property can additionally be utilized to modify the number of elements present in an array. To adjust an array's length, it is necessary to employ the assignment operator along with the length property.
In JavaScript, the property array.length serves a purpose similar to the array.size method found in jQuery. Unlike jQuery, where you can utilize the array.size method, JavaScript does not support this method. Therefore, to determine the size of an array in JavaScript, you should use the array.length property.
Syntax
The syntax outlined below is utilized to obtain the length of an array.
array.length
The syntax below is employed to define the size of an array.
array.length = number
To enhance comprehension, let us examine several examples that demonstrate the utilization of the array.length property.
Example1
This serves as a straightforward illustration for comprehending the method of determining the size of an array by utilizing the array.length attribute.
<html>
<head>
<title> array.length </title>
</head>
<body>
<h3> Here, we are finding the length of an array. </h3>
<script>
var arr = new Array( 100, 200, 300, 400, 500, 600 );
document.write(" The elements of array are: " + arr);
document.write(" <br>The length of the array is: " + arr.length);
</script>
</body>
</html>
Output
The output reveals that the array has a length of six, which exceeds the highest index of the array. In the provided example, the highest index for the specified array is 5.
Example2
In this instance, we are defining the size of an array by utilizing the array.length property. At the outset, the array holds two elements, resulting in an initial length of 2. Subsequently, we expand the array's length to 9.
In the resulting output, the elements of the array are delineated by commas. Upon expanding the array's length, it initially comprises two defined values and seven undefined values, all separated by commas. Subsequently, we proceed to add five elements to the array and display them. At this point, the array will exhibit seven defined values along with two undefined values.
<html>
<head>
<title> array.length </title>
</head>
<body>
<h3> Here, we are setting the length of an array. </h3>
<script>
var arr = [100, 200];
document.write(" Before setting the length, the array elements are: " + arr);
arr.length = 9;
document.write("<br><br> After setting the length, the array elements are: " + arr);
// It will print [ 1, 2, <7 undefined items> ]
arr[2] = 300;
arr[3] = 400;
arr[4] = 500;
arr[5] = 600;
document.write("<br><br> After inserting some array elements: " + arr);
</script>
</body>
</html>
Output
In the following illustration, we will examine the length property of the array when utilizing a non-numeric index.
Example3
In this illustration, the index utilized for the array is non-numeric. This array consists of five elements indexed by non-numeric values. We will utilize the length property on this specific array to observe its behavior. Now, let us examine how the array.length property operates with the array's non-numeric index.
<html>
<head>
<title> array.length </title>
</head>
<body>
<h3> There are five array elements but the index of the array is non numeric. </h3>
<script>
var arr = new Array();
arr['a'] = 100;
arr['b'] = 200;
arr['c'] = 300;
arr['d'] = 400;
arr['e'] = 500;
document.write("The length of array is: " + arr.length);
</script>
</body>
</html>
Output
In the resulting output, it is evident that the length of the array is shown as 0. Following the execution of the code provided above, the output will be -
We can utilize the length property to determine the total count of words within a string. To illustrate this concept, let's consider an example.
Example4
In this illustration, we utilize the length property to show the count of words contained within the string. In this case, we generate an array and employ the split method for its elements. We are dividing the string at each whitespace (" ") character.
When we directly utilize the length property on a string, it provides us with the total count of characters contained within that string. However, in this particular example, we will explore the method for determining the number of words present in the string.
<html>
<head>
<title> array.length </title>
</head>
<body>
<script>
var str = "Welcome to the logic-practice.com";
var arr = new Array();
arr = str.split(" ");
document.write(" The given string is: " + str);
document.write("<br><br> Number Of Words: "+ arr.length);
document.write("<br><br> Number of characters in the string: " + str.length);
</script>
</body>
</html>
Output