JavaScript's for vs for-in vs for-of in Depth

Javascript has several types of “for” loops. here we are going to look each one bit deeper…

Shalitha Suranga
The Startup

--

A loop — Image credits: unsplash.com

There are several loop statements in Javascript but in most cases everyone tends to use for statements because we usually deal with looping scenarios which consist of known conditions with counters or we just need to iterate through a list of data elements.

Eg:- Making an array of numbers for given start and end.

In this simple example starting and ending is clearly pre-defined also it is related with counters(going from start to end). Let’s look into types of for

for loop…

This is the well known basic for loop which is having the following format.

for([initialExpression]; [endingCondition]; [incrementalExpression])
// repetitive statements...

Importantly, above expressions which reside inside for are optional. Basically this simple for loop’s repetitive statements will be executed until [endingCondition] returns false (if it is not provided Javascript assumes it as true in order to continue the execution of the loop). consider the following examples that demonstrate several situations of for loop…

--

--

Shalitha Suranga
The Startup

Programmer | Author of Neutralinojs | Technical Writer