Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Fix the driver behind crashes, sound loss and screen glitches3Repair Windows errors before they cause bigger problemsTo print an array’s length, use the syntax for its language: JavaScript uses array.length, Java uses array.length, and 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
JavaScript: print the length property
JavaScript arrays expose length as a property, not a method. Read it without parentheses and pass the result to console.log:
As an Amazon Associate I earn from qualifying purchases.
const colors = ["red", "green", "blue"];
console.log(colors.length); // 3
The value reflects the array’s highest index plus one, not necessarily the number of populated elements. A sparse array can contain empty slots; for example, assigning a value at index 100 makes its length 101 even if the earlier positions are empty. See MDN’s Array.length reference.
Quick wins for a faster PC:
Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Clear out junk files and repair common Windows errorsFree Scan →Scan for outdated or missing drivers - takes under a minuteDriver Scan →The property is writable, so assigning to it changes the array: reducing the length truncates the array and deletes elements beyond the new length, while increasing it creates empty slots. An invalid length, such as a negative value, throws a RangeError. These are mutations, not just ways to change what gets printed. Details are in MDN’s Array.length reference.
#1 Best Overall
Java: print the fixed array length
Java arrays have a length field. Use it in an output statement such as System.out.println:
int[] values = {4, 8, 12};
System.out.println(values.length); // 3
An array’s length is fixed when it is created. To work with a different size, create a new array or use a collection designed to grow. The Java Language Specification, Chapter 10, defines the length as the number of array components and explains that an empty array has zero components.
Rank #2
C++17: use std::size
For a built-in array or a supported container, C++17’s std::size returns the element count:
The Tool Desk
Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →#include <iostream>
#include <iterator>
int values[] = {4, 8, 12};
std::cout << std::size(values); // 3
The <iterator> header provides std::size. It returns the container’s size() or, for a built-in array, its extent. Consult the C++ reference for std::size for supported forms.
Why sizeof is not a substitute
sizeof reports storage in bytes, not an element count. Applied to a pointer, it reports the pointer’s size and does not recover the number of elements in the original array. Use std::size when you need the element count for a supported array or container.
Quick Recap
Best Value
Rank #4
Quick comparison
| Language | How to print the length | What the value describes | Can the array’s length change? |
|---|---|---|---|
| JavaScript | console.log(array.length) |
Highest array index plus one; sparse slots count toward length. | Yes. Changing length truncates the array or adds empty slots. |
| Java | System.out.println(array.length) |
Number of array components. | No. The length is fixed when the array is created. |
| C++17 | std::cout << std::size(array) |
Element count for built-in arrays and supported containers. | Depends on the array or container type; std::size reports its count. |
Choose the syntax for your language
- In JavaScript, use the
lengthproperty without parentheses. - In Java, use the array’s
lengthfield; the size cannot be changed after creation. - In C++17, use
std::sizefor a built-in array or supported container, rather than treatingsizeofas an element counter.
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.




