How to find the total number of elements in an array in TypeScript?


In this tutorial, we will learn to find the total number of elements in the array. In TypeScript, the array is the data structure containing various elements such as integers, strings, objects, etc.

While working with the APIs and performing operations with the data often requires counting the total number of elements in the array. Also, sometimes it requires counting the number of elements in the array based on certain conditions.

Here, we have focused on various methods to count a total number of elements in the array.

Using the length property of the array in TypeScript

The array prototype contains the length property. When we invoke the length property by taking the array as a reference, it returns the total number of elements in the array.

Syntax

Users can follow the syntax below to use the `length` property of the array to get the total number of elements of the array.

let strArray:Array<string> = ["welcome","to","the","TutorialsPoint!"];
let len = strArray.length;

In the above syntax, we have used the array length property.

Example

In the example below, we have created the array named `strArray`, which contains some string elements. We have used the ‘length’ property of the array to count the total number of elements.

let strArray:Array<string> = ["welcome","to","the","TutorialsPoint!"];
// get array length
console.log("Total elements in the array is " + strArray.length);

On compiling, it will generate the following JavaScript code −

var strArray = ["welcome", "to", "the", "TutorialsPoint!"];
// get array length
console.log("Total elements in the array is " + strArray.length);

Output

The above code will produce the following output −

Total elements in the array is 4

In the output, users can observe that the ‘length’ property returns the ‘4’ as the array contains a total 4 different elements.

Using the for…in loop

In this method, we will iterate through every element of the array using the for-in loop. We can create a variable to store array length and initialize with 0. After that, we can use the for-in loop and increase the values of the array length variable by 1 for every element of the array.

Syntax

Users should follow the syntax below to count the total number of elements using the for-in loop.

let arr:Array<any> = [300, "TutorialsPoint", { age: 44 }];;
let arrayLen:number = 0;
for(let ele in arr){
    arrayLen++;
}

In the above syntax, we have created an array and iterating through every element of an array using the for-in loop. While iterating through the array elements, we are increasing the array length variable.

Example

As the syntax explains, we have created the `arr`, which contains the integer, string, boolean, and objects. Also, we have initialized the ‘arrayLen’ variable with 0. While iterating through the array using for in loop, we are increasing the count of ‘arrayLen’ by 1.

Users should access the ‘arrayLen’ variable to get a total number of elements in the array once iteration through the array completes.

// creating the array of different data types
let arr:Array<any> = [300, "TutorialsPoint", { age: 44 }];
// get array lenght and add to HTML document
let arrayLen:number = 0;
for(let ele in arr){
    arrayLen++;
}
console.log("Total elements in the array is " + arrayLen);

On compiling, it will generate the following JavaScript code −

// creating the array of different data types
var arr = [300, "TutorialsPoint", { age: 44 }];
// get array lenght and add to HTML document
var arrayLen = 0;
for (var ele in arr) {
   arrayLen++;
}
console.log("Total elements in the array is " + arrayLen);

Output

The above code will produce the following output −

Total elements in the array is 3

In the above output, users can observe that the object is counted as a single element and the method returns 3 as the total number of elements.

Also, users can count the number of array elements based on certain conditions. For example, users can count the total number of unique elements in the array, the total number of string elements, and the total number of objects. So, users can count the elements according to requirements, and they have to use the different if-else conditions.

Updated on: 16-Dec-2022

16K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements