Javascript For Loops
Loop classic
You know it, you love it, the loop classic:
let fruits = ["apple", "banana", "jackfruit"]; for (let i = 0; i < fruits.length; i++) { console.log(fruits[i].slice()); }It works but isn’t there something nicer to use out there?
for…in
for...in is not what you are looking for. You might think you can do this:
// BAD CODE let fruits = ["apple", "banana", "jackfruit"]; for (let fruit in fruits) { console.log(fruit.slice()); }But this prints the following:
0 1 2It prints the index of each item because the for...in loop iterates through an object’s enumerable properties. For an object, that iterates through the object’s keys. For an Array, that is actually the index.
We can fix the code by doing the following:
let fruits = ["apple", "banana", "jackfruit"]; for (let fruit in fruits) { console.log(fruits[fruit].slice()); }But now we have a super confusing line there: fruits[fruit]
Add to that, Mozilla explicitly warns users not to use for...in if the order of the elements matters.
Why, JavaScript?
for…of
for...of is actually closer to what you’d expect from a simple loop operator. It works like this:
let fruits = ["apple", "banana", "jackfruit"]; for (let fruit of fruits) { console.log(fruit.slice()); }This outputs what you would expect (hope at this point).
One major snag though, you do not have access to the key, which in this case is the index that for...in was printing. It’s pretty common to need the index so this cannot be the solution I always reach for.
forEach
forEach is a method that can be called on Arrays. It looks like this in the wild:
var fruits = ["apple", "banana", "jackfruit"]; fruits.forEach(function(fruit) { console.log(fruit.slice()); });Slice pun aside, this is pretty simple right? It isn’t the most elegant syntax necessarily but it reads very well and it seems like it’s going to do what it says it’s going to do. Does this means it can be the tool you always reach for? For many yes, but there is one gotcha that breaks the deal for me. You cannot a break or continue operator within the loop and the return statement does nothing whatsoever.
I was so close to just accepting forEach but that snag was big enough that I could never feel truly secure reaching for it 100% of the time.
Back to loop classic
That’s right. The OG for (var i=0; i < array.length; i++) syntax won in the end. Between the rapport I already had with this operator and the fact that every JavaScript developer I know is very familiar with it, I decided to stop seeking greener pastures and stick with what I knew.
Bonus
Honestly though, if it’s reasonable to include a third party library, lodash’s loops will solve all of your problems. They look just like forEach but you can use break, continue, and they are significantly faster.
let fruits = ["apple", "banana", "jackfruit"]; _.forEach(fruits, function(element, i) { console.log(element); });Loop classic
You know it, you love it, the loop classic:
let fruits = ["apple", "banana", "jackfruit"]; for (let i = 0; i < fruits.length; i++) { console.log(fruits[i].slice()); }It works but isn’t there something nicer to use out there?
for…in
for...in is not what you are looking for. You might think you can do this:
// BAD CODE let fruits = ["apple", "banana", "jackfruit"]; for (let fruit in fruits) { console.log(fruit.slice()); }But this prints the following:
0 1 2It prints the index of each item because the for...in loop iterates through an object’s enumerable properties. For an object, that iterates through the object’s keys. For an Array, that is actually the index.
We can fix the code by doing the following:
let fruits = ["apple", "banana", "jackfruit"]; for (let fruit in fruits) { console.log(fruits[fruit].slice()); }But now we have a super confusing line there: fruits[fruit]
Add to that, Mozilla explicitly warns users not to use for...in if the order of the elements matters.
Why, JavaScript?
for…of
for...of is actually closer to what you’d expect from a simple loop operator. It works like this:
let fruits = ["apple", "banana", "jackfruit"]; for (let fruit of fruits) { console.log(fruit.slice()); }This outputs what you would expect (hope at this point).
One major snag though, you do not have access to the key, which in this case is the index that for...in was printing. It’s pretty common to need the index so this cannot be the solution I always reach for.
forEach
forEach is a method that can be called on Arrays. It looks like this in the wild:
var fruits = ["apple", "banana", "jackfruit"]; fruits.forEach(function(fruit) { console.log(fruit.slice()); });Slice pun aside, this is pretty simple right? It isn’t the most elegant syntax necessarily but it reads very well and it seems like it’s going to do what it says it’s going to do. Does this means it can be the tool you always reach for? For many yes, but there is one gotcha that breaks the deal for me. You cannot a break or continue operator within the loop and the return statement does nothing whatsoever.
I was so close to just accepting forEach but that snag was big enough that I could never feel truly secure reaching for it 100% of the time.
Back to loop classic
That’s right. The OG for (var i=0; i < array.length; i++) syntax won in the end. Between the rapport I already had with this operator and the fact that every JavaScript developer I know is very familiar with it, I decided to stop seeking greener pastures and stick with what I knew.
Bonus
Honestly though, if it’s reasonable to include a third party library, lodash’s loops will solve all of your problems. They look just like forEach but you can use break, continue, and they are significantly faster.
let fruits = ["apple", "banana", "jackfruit"]; _.forEach(fruits, function(element, i) { console.log(element); });Loop classic
You know it, you love it, the loop classic:
let fruits = ["apple", "banana", "jackfruit"]; for (let i = 0; i < fruits.length; i++) { console.log(fruits[i].slice()); }It works but isn’t there something nicer to use out there?
for…in
for...in is not what you are looking for. You might think you can do this:
// BAD CODE let fruits = ["apple", "banana", "jackfruit"]; for (let fruit in fruits) { console.log(fruit.slice()); }But this prints the following:
0 1 2It prints the index of each item because the for...in loop iterates through an object’s enumerable properties. For an object, that iterates through the object’s keys. For an Array, that is actually the index.
We can fix the code by doing the following:
let fruits = ["apple", "banana", "jackfruit"]; for (let fruit in fruits) { console.log(fruits[fruit].slice()); }But now we have a super confusing line there: fruits[fruit]
Add to that, Mozilla explicitly warns users not to use for...in if the order of the elements matters.
Why, JavaScript?
for…of
for...of is actually closer to what you’d expect from a simple loop operator. It works like this:
let fruits = ["apple", "banana", "jackfruit"]; for (let fruit of fruits) { console.log(fruit.slice()); }This outputs what you would expect (hope at this point).
One major snag though, you do not have access to the key, which in this case is the index that for...in was printing. It’s pretty common to need the index so this cannot be the solution I always reach for.
forEach
forEach is a method that can be called on Arrays. It looks like this in the wild:
var fruits = ["apple", "banana", "jackfruit"]; fruits.forEach(function(fruit) { console.log(fruit.slice()); });Slice pun aside, this is pretty simple right? It isn’t the most elegant syntax necessarily but it reads very well and it seems like it’s going to do what it says it’s going to do. Does this means it can be the tool you always reach for? For many yes, but there is one gotcha that breaks the deal for me. You cannot a break or continue operator within the loop and the return statement does nothing whatsoever.
I was so close to just accepting forEach but that snag was big enough that I could never feel truly secure reaching for it 100% of the time.
Back to loop classic
That’s right. The OG for (var i=0; i < array.length; i++) syntax won in the end. Between the rapport I already had with this operator and the fact that every JavaScript developer I know is very familiar with it, I decided to stop seeking greener pastures and stick with what I knew.
Bonus
Honestly though, if it’s reasonable to include a third party library, lodash’s loops will solve all of your problems. They look just like forEach but you can use break, continue, and they are significantly faster.
let fruits = ["apple", "banana", "jackfruit"]; _.forEach(fruits, function(element, i) { console.log(element); });Loop classic
You know it, you love it, the loop classic:
let fruits = ["apple", "banana", "jackfruit"]; for (let i = 0; i < fruits.length; i++) { console.log(fruits[i].slice()); }It works but isn’t there something nicer to use out there?
for…in
for...in is not what you are looking for. You might think you can do this:
// BAD CODE let fruits = ["apple", "banana", "jackfruit"]; for (let fruit in fruits) { console.log(fruit.slice()); }But this prints the following:
0 1 2It prints the index of each item because the for...in loop iterates through an object’s enumerable properties. For an object, that iterates through the object’s keys. For an Array, that is actually the index.
We can fix the code by doing the following:
let fruits = ["apple", "banana", "jackfruit"]; for (let fruit in fruits) { console.log(fruits[fruit].slice()); }But now we have a super confusing line there: fruits[fruit]
Add to that, Mozilla explicitly warns users not to use for...in if the order of the elements matters.
Why, JavaScript?
for…of
for...of is actually closer to what you’d expect from a simple loop operator. It works like this:
let fruits = ["apple", "banana", "jackfruit"]; for (let fruit of fruits) { console.log(fruit.slice()); }This outputs what you would expect (hope at this point).
One major snag though, you do not have access to the key, which in this case is the index that for...in was printing. It’s pretty common to need the index so this cannot be the solution I always reach for.
forEach
forEach is a method that can be called on Arrays. It looks like this in the wild:
var fruits = ["apple", "banana", "jackfruit"]; fruits.forEach(function(fruit) { console.log(fruit.slice()); });Slice pun aside, this is pretty simple right? It isn’t the most elegant syntax necessarily but it reads very well and it seems like it’s going to do what it says it’s going to do. Does this means it can be the tool you always reach for? For many yes, but there is one gotcha that breaks the deal for me. You cannot a break or continue operator within the loop and the return statement does nothing whatsoever.
I was so close to just accepting forEach but that snag was big enough that I could never feel truly secure reaching for it 100% of the time.
Back to loop classic
That’s right. The OG for (var i=0; i < array.length; i++) syntax won in the end. Between the rapport I already had with this operator and the fact that every JavaScript developer I know is very familiar with it, I decided to stop seeking greener pastures and stick with what I knew.
Bonus
Honestly though, if it’s reasonable to include a third party library, lodash’s loops will solve all of your problems. They look just like forEach but you can use break, continue, and they are significantly faster.
let fruits = ["apple", "banana", "jackfruit"]; _.forEach(fruits, function(element, i) { console.log(element); });Loop classic
You know it, you love it, the loop classic:
let fruits = ["apple", "banana", "jackfruit"]; for (let i = 0; i < fruits.length; i++) { console.log(fruits[i].slice()); }It works but isn’t there something nicer to use out there?
Loop classic
You know it, you love it, the loop classic:
let fruits = ["apple", "banana", "jackfruit"]; for (let i = 0; i < fruits.length; i++) { console.log(fruits[i].slice()); }It works but isn’t there something nicer to use out there?
Loop classic
for…in
for...in is not what you are looking for. You might think you can do this:
// BAD CODE let fruits = ["apple", "banana", "jackfruit"]; for (let fruit in fruits) { console.log(fruit.slice()); }But this prints the following:
0 1 2It prints the index of each item because the for...in loop iterates through an object’s enumerable properties. For an object, that iterates through the object’s keys. For an Array, that is actually the index.
We can fix the code by doing the following:
let fruits = ["apple", "banana", "jackfruit"]; for (let fruit in fruits) { console.log(fruits[fruit].slice()); }But now we have a super confusing line there: fruits[fruit]
Add to that, Mozilla explicitly warns users not to use for...in if the order of the elements matters.
Why, JavaScript?
for…in
for...in is not what you are looking for. You might think you can do this:
// BAD CODE let fruits = ["apple", "banana", "jackfruit"]; for (let fruit in fruits) { console.log(fruit.slice()); }But this prints the following:
0 1 2It prints the index of each item because the for...in loop iterates through an object’s enumerable properties. For an object, that iterates through the object’s keys. For an Array, that is actually the index.
We can fix the code by doing the following:
let fruits = ["apple", "banana", "jackfruit"]; for (let fruit in fruits) { console.log(fruits[fruit].slice()); }But now we have a super confusing line there: fruits[fruit]
Add to that, Mozilla explicitly warns users not to use for...in if the order of the elements matters.
Why, JavaScript?
for…in
for…of
for...of is actually closer to what you’d expect from a simple loop operator. It works like this:
let fruits = ["apple", "banana", "jackfruit"]; for (let fruit of fruits) { console.log(fruit.slice()); }This outputs what you would expect (hope at this point).
One major snag though, you do not have access to the key, which in this case is the index that for...in was printing. It’s pretty common to need the index so this cannot be the solution I always reach for.
for…of
for...of is actually closer to what you’d expect from a simple loop operator. It works like this:
let fruits = ["apple", "banana", "jackfruit"]; for (let fruit of fruits) { console.log(fruit.slice()); }This outputs what you would expect (hope at this point).
One major snag though, you do not have access to the key, which in this case is the index that for...in was printing. It’s pretty common to need the index so this cannot be the solution I always reach for.
for…of
forEach
forEach is a method that can be called on Arrays. It looks like this in the wild:
var fruits = ["apple", "banana", "jackfruit"]; fruits.forEach(function(fruit) { console.log(fruit.slice()); });Slice pun aside, this is pretty simple right? It isn’t the most elegant syntax necessarily but it reads very well and it seems like it’s going to do what it says it’s going to do. Does this means it can be the tool you always reach for? For many yes, but there is one gotcha that breaks the deal for me. You cannot a break or continue operator within the loop and the return statement does nothing whatsoever.
I was so close to just accepting forEach but that snag was big enough that I could never feel truly secure reaching for it 100% of the time.
forEach
forEach is a method that can be called on Arrays. It looks like this in the wild:
var fruits = ["apple", "banana", "jackfruit"]; fruits.forEach(function(fruit) { console.log(fruit.slice()); });Slice pun aside, this is pretty simple right? It isn’t the most elegant syntax necessarily but it reads very well and it seems like it’s going to do what it says it’s going to do. Does this means it can be the tool you always reach for? For many yes, but there is one gotcha that breaks the deal for me. You cannot a break or continue operator within the loop and the return statement does nothing whatsoever.
I was so close to just accepting forEach but that snag was big enough that I could never feel truly secure reaching for it 100% of the time.
forEach
Back to loop classic
That’s right. The OG for (var i=0; i < array.length; i++) syntax won in the end. Between the rapport I already had with this operator and the fact that every JavaScript developer I know is very familiar with it, I decided to stop seeking greener pastures and stick with what I knew.
Back to loop classic
That’s right. The OG for (var i=0; i < array.length; i++) syntax won in the end. Between the rapport I already had with this operator and the fact that every JavaScript developer I know is very familiar with it, I decided to stop seeking greener pastures and stick with what I knew.
Back to loop classic
Bonus
Honestly though, if it’s reasonable to include a third party library, lodash’s loops will solve all of your problems. They look just like forEach but you can use break, continue, and they are significantly faster.
let fruits = ["apple", "banana", "jackfruit"]; _.forEach(fruits, function(element, i) { console.log(element); });Bonus
Honestly though, if it’s reasonable to include a third party library, lodash’s loops will solve all of your problems. They look just like forEach but you can use break, continue, and they are significantly faster.
let fruits = ["apple", "banana", "jackfruit"]; _.forEach(fruits, function(element, i) { console.log(element); });