JavaScript Tutorial

Javascript For Loop

The for loop is a commonly used loop in JavaScript to iterate over a block of code a specified number of times. It has the following syntax:

Initialization step

This initialization step initializes the loop. The initialization expression is executed only once when the loop starts. We typically use the initialization is to initialize a counter variable.

If we use the var keyword to declare the counter variable, the variable will have either function or global scope. In other words, we can reference the counter variable after the loop ends.

However, if we use the let keyword to declare the counter variable, the variable will have a blocked scope, which is only accessible inside the loop.

Condition step

This conditional step is an expression that is evaluated once before every iteration. The statement inside the loop is executed only when the condition evaluates to true.

The loop is terminated if the condition evaluates to false. Note that the condition is optional. If we omit it, the for loop statement considers it true.

Post-expression step

This for loop statement also evaluates the post-expression after each loop iteration. Generally, we use the post-expression to update the counter variable.

Input:-

Output:-

Javascript For In loop

The JavaScript for...in loop is used to iterate over the properties of an object. It allows you to loop through the enumerable properties of an object, including its inherited properties. Here is an explanation of how the for...in loop works:

Following is the example to illustrate this loop:-

Output:-

For each() loop

I apologize for the confusion, but there is no built-in forEach() loop in JavaScript. However, there is an array method called forEach() that allows you to iterate over the elements of an array. Here is an explanation of how the forEach() method works:

Input:-

Output:-

The following illustrates the syntax of the forEach() method.

array.forEach(callback[, thisArg]);

The forEach() method takes two arguments:

  1. callback: A callback function that is executed for each element in the array. This function can take up to three arguments:

    • currentElement: Represents the current element being processed.
    • index (optional): Represents the index of the current element being processed.
    • array (optional): The array on which the forEach() method was called.
  2. thisArg (optional): An object to use as this when executing the callback function.

It's important to note that the forEach() method does not return anything (i.e., it returns undefined), and it is not chainable with other array methods like filter(), map(), some(), every(), and sort().

Javascript Loop For Of

It is used to iterate over iterable objects, such as arrays, strings, maps, and more. The syntax for the for...of loop is as follows:

The above syntax has two arguments:- variable and iterable.

The variable is declared using let, const, or var, and it represents the value of the current iteration. The iterable is an object that has iterable properties.

Input:-

Output:-

 

Go back to Previous Course