JS Startup

Challenges, Tips & Tricks & Tutorials

JavaScript Quiz – for loop iterate array in two ways

JavaScript Quiz – for loop iterate array in two ways

for loop iterate array

for loop is a javascript loop which repeat certain block of code. In the same way javascript introduce two new array which are for…of and for..in. It’s a new and easy way to iterate array compare to normal for loop.

Basic syntax of for..in and for..of

// for...in Loop
for (variable in iterable) { 
statement 
}

// for...of Loop
for (variable of iterable) { 
statement 
}

For more detail about javascript loop.

JavaScript Question

In this javascript quiz, find out for loop iterate array in two ways which are for in and for of loop introduce in ES6.

First, we declare an variable array using let keyword and assign an array to it.

let array = [1];

Now, we use for of loop and printing that value of val.

for(let val of array){
  console.log(val); // output => 🧐 ?
}

Again, we printing the value val but it from for in loop.

for(let val in array){
  console.log(val); // output => 🧐 ?
}

So, Any idea what should be the output of above code statement.

JavaScript Quiz Test

Here is the full javascript code snippet for you to solve.

let array = [1];

for(let val of array){
  console.log(val); // output => 🧐 ?
}
for(let val in array){
  console.log(val); // output => 🧐 ?
}

// Made with ❤️, by @jsstartup

Answer – 1, “0”

Click To Run Code

Explanation

Now, you know the answer is 1, “0”. But why, doesn’t it should give the same answer. Let me explain.

Make sure to go through our detail post on javascript loop which will let you know the answer of this blog post.

InShort: for..of iterate over array value and for..in iterate over array index.

So, when for..of iterate over array it will print array value like 1 in our case. In that same, for..in iterate over array index or property at which it’s value store.

At last, That’s why we get an answer 1, “0“.

Let me know in the comment section, what you think about it.

I hope you understand the concept and logic behind it.

But wait, i have something more for you –

Check out our other javascript quiz – 

  1. JS Quiz – can array have the size method like a set
  2. JS Quiz – access JSON string value is possible
  3. JavaScript Quiz – can divide assignment operator work in one go
  4. JavaScript Quiz – do act then while checking falsy

Conclusion

To participate in our javascript quiz or challenges, tutorial, tips & tricks make sure to join our jsstartup newsletter. So, you can able to participate in our daily challenges & learn the javascript concept.

And last but not the least, don’t forget to like, comment and share. It gives us a morale boost to remain to continue. Also, join our Facebook Page

If you have any questions, please feel free to ask me in the comment section and also let me know if you have any suggestions. As suggestions are always welcome.

Leave a Reply

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

Back to top
0 Shares
Share via
Copy link