JavaScript and JQuery Essentials Trainin... (15 Blogs) Become a Certified Professional

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

43 Views

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assement report and video recording

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.

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.

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

Comments
0 Comments

Browse Categories

Subscribe to our Newsletter, and get personalized recommendations.