Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

To print an array’s length, use the syntax for its programming language: JavaScript and Java use array.length, while C++17 provides std::size(array) for built-in arrays and supported containers. For example, in JavaScript:

const items = ["a", "b", "c"];
console.log(items.length); // 3

Print an array’s length in JavaScript

JavaScript arrays have a length property, not a length() method. Read the property and pass it to console.log:

const colors = ["red", "green", "blue"];
console.log(colors.length); // 3

The value reflects the highest array index plus one, not necessarily the number of populated slots. For instance, assigning a value at index 100 gives the array a length of 101 even if the earlier positions are empty. See MDN’s Array.length reference.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The property is writable, so changing it also changes the array: reducing the length deletes elements beyond the new endpoint; increasing it adds empty slots. Invalid lengths, such as a negative number, cause a RangeError. Details are in MDN’s Array.length reference.

Print an array’s length in Java

Java arrays expose a final instance variable named length. Print it with an output statement such as System.out.println:

int[] values = {4, 8, 12};
System.out.println(values.length); // 3

The length is fixed when the array is created. If you need a different size, create another array or use a collection type designed to grow. The Java Language Specification defines an array of length n as having components indexed from 0 through n - 1; an array may also be empty. See Java Language Specification, Chapter 10 and Section 10.2, Array Variables.

Print an array’s length in C++17

In C++17, use std::size for a built-in array or a supported container. Include <iterator> and print the returned element count:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#include <iostream>
#include <iterator>

int values[] = {4, 8, 12};
std::cout << std::size(values); // 3

std::size returns a container’s size() or the extent of a built-in array. It counts elements, whereas sizeof reports storage in bytes. Applying sizeof to a pointer does not reveal the original array’s element count. See the C++ reference for std::size.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Which syntax should you use?

Language How to print the count What it reports Can the array length change?
JavaScript console.log(array.length) Highest array index plus one; sparse slots count toward the length. Yes. Changing length truncates the array or creates empty slots.
Java System.out.println(array.length) The array’s number of components. No. The length is fixed when the array is created.
C++17 std::cout << std::size(array) Element count for a built-in array or supported container. Depends on the array or container type.

These examples cover JavaScript, Java, and C++17. The syntax differs across languages, so do not assume that .length or std::size applies to a language not covered here.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.