. Advertisement .
..3..
. Advertisement .
..4..
Are you struggling to print array in Javascript? Once you have read these guidelines, such issues will no longer bother you. As is with other tasks, there are many critical approaching methods. However, we would like to introduce only the two most efficient ones to you. Check them out right away!
How to Print Array in Javascript?
There are five methods you can consider:
- Method 1. Use “For Loop”
- Method 2. Use “For Each”
- Method 3. Use “Console. Log()”
- Method 4. Use Map () and Join ()
- Method 5. Use ToString()
Method 1: Use “For Loop.”
Every array element boasts index values that redefine the element located in a specific array. For instance, the first array element yields a particular index value “0”, the second one “1”, the third one “2”, et cetera.
So how can we encounter the first one? You may write array_name[0], array_name[1] for your second element, and continue so for the subsequent segment.
Now, let’s see how you may use “for loop” to print an array. First, activate the “for loop” a few times. For each time, employ an iterator to enter the element array. Print them one by one.
This example will show you how to do it:
let exp_array1 = ['Pascal', 'jQuery', 'Ruby', 'Python', 'JavaScript'];
for (let i = 0; i < exp_array1.length; i++) {
console.log(exp_array1[i]);
}
Output:
Pascal
jQuery
Ruby
Python
JavaScript
Method 2: Use “For-Each.”
The method “ForEach” is also terrific for complete array printing in JavaScript! Our code snippet here will clear the clouds for you.
var exp_array2 = ["Libra", "Aquarius", "Pisces", "Taurus", "Scorpius"];
exp_array2.forEach(object => console.log(object));
Output:
Libra
Aquarius
Pisces
Taurus
Scorpius
Method 3. Use “Console.Log()”
In previous examples, we have shown you how to print complete arrays with one element in each line. How about printing every array element within one single line? Console.Log() can help you with that.
var exp_array3 = ["Mercury", "Jupiter", "Venus", "Uranus", "Scorpius"];
console.log(exp_array3);
Output:
['Mercury', 'Jupiter', 'Venus', 'Uranus', 'Scorpius']
Method 4. Use Map() and Join()
Combining Map() and Join() also helps you print array elements in a line:
var exp_array4 = ["Shark", "Crab", "Dolphin", "Herring", "Salmon"];
var object4 = exp_array4.map( (i,j) => (j+1+"."+i) ).join(' / ');
console.log(object4);
Output:
1.Shark / 2.Crab / 3.Dolphin / 4.Herring / 5.Salmon
Method 5. Use ToString()
toString() will convert your array into strings and print its elements out for you – a great approach if the previous solutions do not appeal to you!
var exp_array5 = ["Ostrich", "Hummingbird", "Peacock", "Heron", "Crow"];
console.log(exp_array5.toString());
Output:
Ostrich,Hummingbird,Peacock,Heron,Crow
Conclusion
Our article has detailed the steps on how to print array in Javascript. Regarding array conversion into strings, you may refer to ITTutoria’s previous article for more information.
ITTutoria hopes that this guide will prove handy, and do not forget to contact us if some facets are still confusing to you! We will strive our best to tackle all your questions.
Leave a comment