In nodeJs is there a way to loop through an array without using array size?

Let's say I have

myArray = ['item1', 'item2']

I tried

for (var item in myArray) {console.log(item)}

It prints 0 1

What I wish is to have item1 item2

Is there any other syntax that works without using

for (var i = 0; i < myArray.length; i++)


You can use Array.forEach

var myArray = ['1','2',3,4]


What you probably want is for...of, a relatively new construct built for the express purpose of enumerating the values of iterable objects:

let myArray = ["a","b","c","d"];
for (let item of myArray) {

To print 'item1' , 'item2', this code would work.

var myarray = ['hello', ' hello again'];

for (var item in myarray) {

In ES5 there is no efficient way to iterate over a sparse array without using the length property. In ES6 you can use for...of. Take this examples:

'use strict';

var arr = ['one', 'two', undefined, 3, 4],

arr[6] = 'five';

output = '';
arr.forEach(function (val) {
    output += val + ' ';

output = '';
for (var i = 0; i < arr.length; i++) {
    output += arr[i] + ' ';

output = '';
for (var val of arr) {
    output += val + ' ';
<!-- results pane console output; see -->
<script src="//"></script>

Use Iterators...

var myarray = ['hello', ' hello again'];
processArray(myarray[Symbol.iterator](), () => {
    console.log('all done')
function processArray(iter, cb) {
    var curr =
        return cb()
    processArray(iter, cb)

More in depth overview:

    var count=0;
    let myArray = '{"1":"a","2":"b","3":"c","4":"d"}'
    var data = JSON.parse(myArray);
    for (let key in data) {
      let value =  data[key]; // get the value by key
      console.log("key: , value:", key, value);
      count = count + 1;

This is the natural javascript option

var myArray = ['1','2',3,4]


Use the built-in Javascript function called map. .map() will do the exact thing you're looking for!

Need Your Help

ZF2 Rest Service/Doctrine Validation

validation zend-framework2

I'm struggling with the best way to validate Doctrine 2 entities in a ZF2 Rest Service. I initially implemented validation by extending Zend\InputFilter\InputFilter and attached validators to the f...

Calendar Script - Mapping ID and Date in the Date Columns

php mysql date

I am creating a Calendar for a guest house. It simply tells the dates Booked or Available.