What is the difference between for in and for of statements

0 votes

I'm familiar with a for... in loop (it iterates over the keys), but this is the first I've heard of a for... of loop (it iterates over values).

I don't understand the for...of loop.

var arr = [3, 5, 7];
arr.foo = "hello";
    
for (var i in arr) {
  console.log(i); // logs "0", "1", "2", "foo"
}
    
for (var i of arr) {
  console.log(i); // logs "3", "5", "7"
  // it doesn't log "3", "5", "7", "hello"
}

I realize that for... of property values iterates. 

So why does it log "3", "5", "7", and "hello" instead of "3", "5", "7"?

Contrary to the for... in loop, which iterates over each key (such as "0," "1," "2," and "foo") as well as the foo key, the for... of loop does not iterate over the value of the foo property, i.e., "hello." 

Why is it the case?

For... of loop, I console here. 

"3", "5", "7," and "hello" are logged instead of "3", "5", and "7." 

Why?

Nov 9, 2022 in Java by Nicholas
• 7,760 points
189 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Java

0 votes
2 answers

What is the difference between Set and List in java?

List is an ordered sequence of elements. ...READ MORE

answered Apr 26, 2018 in Java by Akrati
• 3,190 points
62,682 views
0 votes
2 answers

What is the difference between getAttribute() and getParameter() in java?

getParameter() returns http request parameters. Those passed from ...READ MORE

answered Aug 28, 2019 in Java by Sirajul
• 59,230 points
21,396 views
0 votes
2 answers

What is the difference between Type List and type ArrayList in Java

By List, you actually tell, that your object ...READ MORE

answered Aug 28, 2019 in Java by Sirajul
• 59,230 points
2,246 views
0 votes
1 answer

What is the difference between getPath(), getAbsolutePath() and getCanonicalPath() in Java?

getPath() - returns a String which denotes the ...READ MORE

answered Jul 26, 2018 in Java by Mrunal
• 680 points
1,664 views
0 votes
2 answers

What is the difference between Map and HashMap in java?

The basic difference is : Map is ...READ MORE

answered Aug 28, 2019 in Java by Sirajul
• 59,230 points
28,997 views
0 votes
1 answer

What is the difference between main(String[] args) and main(String... args) in Java?

String[] is used to accept a single parameter ...READ MORE

answered Sep 6, 2019 in Java by Piya
3,418 views
0 votes
0 answers

Convert Array to Object

How can I convert this : ['a','b','c'] To this: { ...READ MORE

May 19, 2022 in Java-Script by Kichu
• 19,050 points
359 views
0 votes
0 answers

Convert Array to Object

What is the best way to convert: ['a','b','c'] to: { ...READ MORE

Sep 20, 2022 in Java by Nicholas
• 7,760 points
341 views
0 votes
0 answers

How do I remove an object from an array with JavaScript?

I have a JavaScript object that looks ...READ MORE

Dec 8, 2022 in Java by Nicholas
• 7,760 points
217 views
0 votes
1 answer

How to store an array in localstorage?

Localstorage only supports Strings. So you can ...READ MORE

answered Jul 1, 2019 in Others by sunshine
• 1,300 points
22,372 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP