Array Length In JavaScript: All You Need To Know About Length Property In JavaScript

Last updated on Apr 05,2024 8.4K Views

Array Length In JavaScript: All You Need To Know About Length Property In JavaScript

edureka.co

Arrays play a very important role when it comes to holding data to be programmed. Likewise JavaScript is a popular scripting language with strong market hold. This article on ‘Array Length In Java’ will help you explore ‘Length’ functions concerning arrays in JavaScript. Following are the pointers this article will touch up on,

Let us start with the first topic of discussion,

Array Length in JavaScript

So how do we calculate the length of array in JavaScript? The length of an array in JavaScript can be found methodically by using  the length property also known as JavaScript Array Length property. The length property can do multiple jobs, including returning or setting the number of elements in the array as it returns an unsigned 32 bit integer.

The value returned is an unsigned integer. The length property can be specified as:

array.length

We can refer to the program below for a better understanding, start with installation first:

Example


<script type="text/javascript">
var music = new Array();
music[0] = "Rock";
music[1] = "Pop";
music[2] = "Jazz";
music[3] = "Blues";
document.write(music.length);
</script>

Output

4

Another way to use the length property is as follows:

Example:

</p>
<script>
//JavaScript to illustrate length property
function fun() {
// length property for array
document.write([9,2,4,8,1,7,6,3,5].length);
document.write("<br>");
// length property for string
document.write("HelloWorld".length)
}
fun();
</script>
<p style="text-align: justify;">

It should be noticed that in case of strings, the length property returns the number of characters present in the string, while in case of arrays, the number of elements present in the array is returned.

Output

9

10

Let us now see how can we display elements using length.

Enhance your design skills with our top-rated UX Design Course.

Displaying the elements using length

In the following program, the last element of the array is accessed by using the following method: (music.length-1), after which the element is displayed.

Exmaple:


<script type="text/javascript">
var music = new Array();
music[0] = "Rock";
music[1] = "Pop";
music[2] = "Jazz";
music[3] = "Blues";
document.write(music[music.length-1]);
</script>

Output:

Blues

Let us move to the last bit of this article and see how can we find out number of words present, with help of length.

Finding out the number of words present using length

Length can be used to find out the number of words present in a string. We also make use of the split function to create an array.


<script type="text/javascript">
//creating a string
var str="Music is a great way to relax. And rejuvenate";
//creating an array
var arr = new Array();
//using the split function
arr=str.split(" ");
document.write("Total Number Of Words: "+ arr.length);
</script>

Output:

Total Number Of Words: 9

Finding out the length of an array is not only a fundamental requirement, but also holistic approach to many problems.

With this we come to the end of this blog on ‘Array Length In JavaScript’. I hope you found this informative and helpful, stay tuned for more tutorials on similar topics.You may also checkout our training program to get in-depth knowledge on jQuery along with its various applications, you can enroll here for live online training with 24/7 support and lifetime access.

Check out the Angular Course by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Angular is a JavaScript framework that is used to create scalable, enterprise, and performance client-side web applications. With Angular framework adoption being high, performance management of the application is community-driven indirectly driving better job opportunities.

If you want to get trained in React and wish to develop interesting UI’s on your own, then check out the Best React Course by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.

Got a question for us? Mention them in the comments section of  this blog and we will get back to you.

Upcoming Batches For UI UX Design Certification Course
Course NameDateDetails
UI UX Design Certification Course

Class Starts on 22nd February,2025

22nd February

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES