JavaScript array.length property17 Mar 2025 | 4 min read The length property returns the number of elements in an array in the form of a 32-bit unsigned integer. We can also say that the length property returns a number that represents the number of array elements. The return value is always larger than the highest array index. The length property can also be used to set the number of elements in an array. We have to use the assignment operator in conjunction with the length property to set an array's length. The array.length property in JavaScript is same as the array.size() method in jQuery. In JavaScript, it is invalid to use array.size() method so, we use array.length property to calculate the size of an array. SyntaxThe following syntax is used to return the length of an array The following syntax is used to set the length of an array For better understanding, let's see some of the illustrations of using array.length property. Example1It is a simple example to understand how to calculate the length of an array using the array.length property. Output In the output, we can see that length of the array is six, which is greater than the value of array's highest index. The highest index of the specified array in the above example is 5. ![]() Example2In this example, we are setting the length of an array by using the array.length property. Initially, the array contains two elements, so at the beginning, the length is 2. Then we increase the length of the array to 9. In the output, the values of the array are separated by commas. After increasing the length, the array contains two defined and seven undefined values separated by a comma. Then we insert five array elements and print them. Now, the array contains seven defined and two undefined values. Output ![]() In the next example, we will test the length property on the array with the non-numeric index. Example3In this example, the index of the array is non-numeric. Here, the array contains five elements with the non-numeric index. We are applying the length property on the given array to see the effect. Now let's see how the array.length property works on the non-numeric index of the array. Output In the output, we can see that the length of the array is displayed 0. After the execution of the above code output will be - ![]() We can also use the length property to find out the number of words in the string. Let's understand it with an example. Example4In this example, we are using the length property to display the number of words present in the string. Here, we are creating an array and use the split() function for the array elements. We are splitting the string from the whitespace (" ") character. If we direct apply the length property on the string, then it gives us the number of characters in the string. But in this example, we will understand how to calculate the number of words in the string. Output ![]() Next TopicJavaScript alert() |
String comparison is a foundational aspect of programming. In particular, web development frequently depends on user input, data validation, and conditional logic. It is where string comparison is often utilized. A trusted language, JavaScript has many methods to compare strings. This article delves into different techniques...
3 min read
Sometimes we need to calculate the current week number or the week number for a given date. This problem can be solved using the JavaScript programming language. JavaScript offers several date functions, such as getDays(), getMonth(), getTime(), to solve date-related tasks. Along with that, the...
7 min read
? Radio button is an input type that is used to get input from the user by selecting one value from multiple choices. You have seen the radio buttons to choose gender between male and female. We select only one entry, either male or female and...
4 min read
? Annotations, commands, and comments that accompany code are important semantics in documents in JavaScript. These can be special gear, external files, or car-generated documents with instant memos in code. The motivation behind Documentation is to make the code understandable to end customers who may need to...
6 min read
play a crucial role in managing asynchronous data in contemporary web and server-side applications. They enable developers to efficiently handle data flows by breaking down data into smaller pieces instead of processing them in large, memory-heavy chunks. This is why streams are especially useful for...
10 min read
JavaScript is a flexible programming language that powers a significant part of the intelligent content on the web. Among its heap of highlights and functions, the count() function stands apart as a convenient tool for engineers. In this article, we'll dig into the count() function...
3 min read
What is a Lambda Expression? In JavaScript, a lambda expression is a concise way to define a short function in programming. A lambda expression is commonly found in modern languages like Ruby, JavaScript and Java. In simple words, we can say that it just a small piece...
6 min read
Introduction: JavaScript, the flexible and broadly utilized programming language, assumes a significant part in web development, permitting developers to make dynamic and intelligent websites. To bridle the maximum capacity of JavaScript, developers frequently go to online JavaScript runners. These integral assets work with testing and exploring...
5 min read
In earlier times, JavaScript used to run in the client's browser. As the technology evolved with the time, web development also evolved. Websites were static in older times which means they only showed information but now they have become dynamic consisting of many things to interact...
5 min read
The offsetHeight is an HTML DOM property, which is used by JavaScript programming language. It returns the visible height of an element in pixels that includes the height of visible content, border, padding, and scrollbar if present. The offsetHeight is often used with offsetWidth property....
4 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India