HOWTO · JavaScript
How to Print Array Elements in JavaScript
This tutorial shows different ways to print all or a certain number of JavaScript array elements.
On this page
To print JavaScript array elements, choose between writing each value on its own line, displaying the array, or building a formatted string. Use an indexed loop or forEach for line-by-line output, console.log() to inspect the whole array, and map() with join() when you need numbering or a custom separator. For selected values, use slice(); to combine arrays, use concat(). The endpoint methods pop() and shift() also return values, but remove them from the original array.
Use loops and forEach to print one element per line
An indexed for loop gives direct access to each value and its position. A while loop can do the same when you initialize an index before the loop and increment it on every pass. For a simple action on each value, forEach accepts a callback that receives the current element (and optionally its index). These approaches write one value per call to console.log():
const names = ['mehvish', 'tahir', 'aftab', 'martell'];
names.forEach((name) => console.log(name));
mehvish
tahir
aftab
martell
Choose an indexed loop when later logic needs the index or you need to control iteration. forEach is concise when the task is simply to handle each array element. It returns undefined; it does not create a transformed array.
Print the whole array with console.log()
Pass the array itself to console.log() to inspect all its elements in one call. This is useful while debugging, but the display is a runtime console representation; browsers and Node.js may format arrays differently. If another part of a program needs a predictable string, convert the values with join() instead.
const names = ['mehvish', 'tahir', 'aftab', 'martell'];
console.log(names);
[ 'mehvish', 'tahir', 'aftab', 'martell' ]
console.log() is a method on console, while forEach, map, and the conversion methods below are array methods. The console display is not a serialized format for files or network data.
Format or number elements as a string
Use map() to create a new array of labels, then join() to combine those labels into one string. The callback receives the element and its zero-based index, so adding one produces human-readable numbering. map() leaves the source array unchanged, and join() returns a string.
const names = ['mehvish', 'tahir', 'aftab', 'martell'];
const numberedNames = names.map((name, index) => `${index + 1}. ${name}`);
console.log(numberedNames.join(' | '));
1. mehvish | 2. tahir | 3. aftab | 4. martell
For plain comma-separated text, the toString() method converts an array to a string. It uses commas between elements and does not let you choose a separator. The join() method lets you choose one, such as a space or a vertical bar. Both produce strings rather than printing by themselves; pass the result to console.log() to display it.
Print selected elements with slice()
Call slice(start, end) to make a shallow copy of a portion of an array, then print that returned array. The start index is included and the end index is excluded, so slice(0, 2) selects the first two elements. slice() does not remove values from the original array.
const names = ['mehvish', 'tahir', 'aftab', 'martell'];
console.log(names.slice(0, 2));
[ 'mehvish', 'tahir' ]
The selected values remain in their original order. For the string method that uses similar index arguments, see the slice() reference; here the receiver is an array.
Combine arrays with concat()
Use concat() to create a new array containing the receiver’s elements followed by the supplied array’s elements. Print the result directly to inspect it, or call join() if the output should be a string with a chosen separator. Neither input array is changed.
const fruitsArrayOne = ['apple', 'banana', 'grapes'];
const fruitsArrayTwo = ['mango', 'water-melon'];
const combined = fruitsArrayOne.concat(fruitsArrayTwo);
console.log(combined.join(', '));
apple, banana, grapes, mango, water-melon
The order matters: the array before .concat() comes first in the result. The method can also accept individual values or more arrays as arguments.
Print and remove an endpoint element
Use pop() to get and remove the last element, or shift() to get and remove the first. Each method changes the original array and returns the removed value, which you can pass to console.log(). Use them only when that change is intended; choose slice() or read an index when the source array must remain intact.
const names = ['mehvish', 'tahir', 'aftab', 'martell'];
const lastName = names.pop();
console.log(lastName);
console.log(names.join(', '));
martell
mehvish, tahir, aftab
pop() and shift() are array methods, not string operations. The first call above prints the value that was at the end; the next line shows the remaining array. To inspect the first element while removing it, replace pop() with shift().