• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Using JavaScript to Get the Length of Array

Using JavaScript to Get the Length of Array

September 19, 2022 Leave a Comment

We can use JavaScript to get the length of an array easily by simply using the JavaScript Array length property.

arr.length

And here is a simple example.

var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];

console.log(numbers.length);

#Output
9

When working with arrays, it can be useful to be able to easily calculate the array length and size of an array.

To get the length of an array in JavaScript, we can use the length property.

Let’s see the length property used on a bunch of different arrays.

var numbersArray = [32,1,0,09,23,430,1000];
var stringsArray = ["This","an","array","of","strings"];
var mixedArray = ["Hello","",null,"null",true,234,"23"];
var arr1 = [];
var arr2 = [""];

console.log(numbersArray.length);
console.log(stringsArray.length);
console.log(mixedArray.length);
console.log(arr1.length);
console.log(arr2.length);

#Output
7
5
7
0
1

Using a For Loop to Get the length of Array in JavaScript

Another way we can easily get the length of an array is with a for loop.

Below shows you how to get the length of an array of numbers in JavaScript with a for loop.

var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];

var arrayLength = 0;
for( var i =0; i<numbers.length; i++ ){
  arrayLength++;
}

console.log(arrayLength);

#Output
9

Hopefully this article has been useful for you to learn how to use JavaScript to get the length of array.

Other Articles You'll Also Like:

  • 1.  Using JavaScript to Set the Width of a Div
  • 2.  JavaScript Check If Number is a Whole Number
  • 3.  Using JavaScript to Add Leading Zeros
  • 4.  How to Convert a String to Lowercase In JavaScript
  • 5.  Using JavaScript to Check if Number is Divisible by Another Number
  • 6.  Using JavaScript to Get the URL Path
  • 7.  Using JavaScript to Convert Float to Integer
  • 8.  JavaScript acosh – Find Hyperbolic Arccosine of Number
  • 9.  Remove Multiple Characters From String in JavaScript
  • 10.  Using JavaScript to Capitalize the First Letter of a String

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy