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() |
The strings are usually used to store and manipulate the text data. However, you can also store special characters and numeric data in strings as well. E.g., address or email id, which contains multitype data. Strings are created by putting data inside the quotes. JavaScript and...
3 min read
The double question mark (??) denotes the nullish coalescing operator introduced in ES6. When its left-hand side operand is undefined or null, the nullish coalescing (??) operator returns its right-hand side operand. In all other cases, the logical operator returns its left-hand side operand. The nullish...
5 min read
The JavaScript Arrow function does not have the "this" value or any argument. Using it as an event handler, an object literal method, or a prototype method is not recommended. If a function applies the arguments objects, the arrow function does not work correctly. The...
4 min read
We will understand the JavaScript URL encode in this article. URL encoding is the procedure of converting characters within a URL into a format that can be transmitted over the internet. URL decoding is the procedure of converting the encoded URL back to its original form. It...
3 min read
We will understand the composition in JavaScript in this article. As the word "composition" means combining two or more things together to compose a new thing. Function Composition In JavaScript, there is a technique called the function composition. This technique is utilized to combine two or more functions...
3 min read
What are Falsy values in JS? In JavaScript, falsy values are considered false when we evaluate in a Boolean context. It means that if any of these values are used in a condition such as if statement, they will be treated as false. With the help of these...
7 min read
A JavaScript built-in object called BigInt and it is representing whole numbers greater than 253-1. The BigInt variables in JavaScript are used to store large integer values that are too large to fit into a regular JavaScript number. The bigint is a primitive type similar to...
5 min read
Reusable code can help developers create applications. Reusing code does not mean using it without the JavaScript function. Code segments are run at runtime by functions. The normal and Arrow functions are the two main uses of JS. In this article, we will discuss the difference between...
10 min read
Immediately Invoked Function Expressions (IIFE, AKA self-invoking) is a weapon one should not neglect to use when it comes to JavaScript programming. These mechanisms are more advanced ones; they kind of promote software security, information confidentiality, and an effective way of programming under this localized...
6 min read
What is an Array? In JavaScript, an array is one of the data structures in which multiple values can be stored in one variable. These values, known as elements, can be of any type numbers, strings or objects. Arrays can play a great function in handling captured...
5 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