• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / JavaScript / JavaScript Print Array – Printing Elements of Array to Console

JavaScript Print Array – Printing Elements of Array to Console

June 6, 2022 Leave a Comment

In JavaScript, we can print an array using a couple of methods. The easiest way to print an array using JavaScript is to use the console.log() method.

The first example shows how to print an array using just the console.log() method.

var anArray = ["This","is","a","array","of","strings"];
console.log(anArray);

#Output:
['This', 'is', 'a', 'array', 'of', 'strings']

If we want to print each element and not have it in an array, we can use a for loop to go through each element in the array and print each one.

var anArray = ["This","is","a","array","of","strings"]
for( var i=0; i<anArray.length; i++ ){
  console.log(anArray[i]);
};

#Output:
This
is
a
array
of
strings

And finally, if we want to print all the items in the array on one line as one long string, we can use the join() method to convert the array to a string and then print it.

var anArray = ["This","is","a","array","of","strings"];
console.log(anArray.join(" "));

#Output:
This is a array of strings

When working with arrays, it can be useful to be able to print the items of an array.

We can easily print the elements of an array in JavaScript using a number of different methods.

We can print an array by using the console.log() method. This will print the array of object to the console. Once again, here is our code to do this:

var anArray = ["This","is","a","array","of","strings"];
console.log(anArray);

#Output:
['This', 'is', 'a', 'array', 'of', 'strings']

Typically, we want to get only the elements of the array, and not the array. To print the items of an array to the console with JavaScript, we use a for loop to print the elements of an array.

Here is our code again to do this:

var anArray = ["This","is","a","array","of","strings"]
for( var i=0; i<anArray.length; i++ ){
  console.log(anArray[i]);
};

#Output:
This
is
a
array
of
strings

Printing the First N Elements of an Array in JavaScript

In JavaScript, we can easily print the first n items in an array. To do so, we can use a for loop as shown in the second example of this article.

To print the first n items of an array, we just loop over those first n elements and print them out.

Below is an example that prints out the first 10 items of an array in JavaScript.


var numbersArray = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]

for( var i=0; i<10; i++ ){
  console.log(numbersArray[i]);
};

#Output:
0
1
2
3
4
5
6
7
8
9

Hopefully this article has been useful for you to understand how to use javascript to print an array.

Other Articles You'll Also Like:

  • 1.  JavaScript Capitalize First Letter of Every Word
  • 2.  Using JavaScript to Change the href of a Link
  • 3.  Using JavaScript to Set the Id of an Element
  • 4.  JavaScript Ceiling – Using Math.ceil() Method to Round Up to Ceiling
  • 5.  Remove Parentheses From String Using JavaScript
  • 6.  JavaScript indexOf – Get the position of a value within a string
  • 7.  Insert Character Into String In JavaScript
  • 8.  How to Get the Cursor Position in JavaScript
  • 9.  JavaScript cos – Find Cosine of Number in Radians Using Math.cos()
  • 10.  JavaScript Map Size – Find Length of JavaScript Map

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy