Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk2 min

Print an Array’s Length: The Right Syntax in JavaScript, Java, and C++

Use JavaScript’s array.length, Java’s array.length, or C++17’s std::size(array) to print an element count—with different rules for sparse and fixed-size arrays.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To 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.

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

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.

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.

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

C++17: use std::size

For a built-in array or a supported container, C++17’s std::size returns the 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

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 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 length property without parentheses.
  • In Java, use the array’s length field; the size cannot be changed after creation.
  • In C++17, use std::size for a built-in array or supported container, rather than treating sizeof as 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.

Leave a Reply

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

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

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.