Javascript - For Statement

About

The for iterator statement

Syntax

for of (array)

array iteration

var colors = [ "blue", "green" ];
  • The colors
console.log("color values are:");
for (const color of colors) {
    console.log("  "+color);
}

for in (property in object)

en-US/docs/Web/JavaScript/Reference/Statements/for...in.

Not for an array

The loop iterate over all enumerable properties of the object itself and those the object inherits from its constructor's prototype (properties closer to the object in the prototype chain override prototypes' properties).

var person = {name: "Nicolas", length: 180};

function ColoredPerson() {
    // Rode piet !
    this.color = "red";
}
ColoredPerson.prototype = person;

var coloredPerson = new ColoredPerson();

var propSource;
for (var prop in coloredPerson) {
    if (coloredPerson.hasOwnProperty(prop)) {
        propSource = "object";
    } else {
        propSource = "prototype";
    }
    console.log("The property " + prop + " (value:" + coloredPerson[prop] + ") is a property from the " + propSource + ".");
}

for ( initialization; test; update )

for (initialization; test; update) {
    //Code to run each time through the loop
}

Let op ! Thanks to variable hoisting and function-level scope, you may create variable name clash because the code below

for (var i = 0; i < 5; i++) {
    console.log(i);
}

is equivalent to:

var i;
for (i = 0; i < 5; i++) {
    console.log(i);
}

Example:

/* To show a variable name clash with the variable in the for loop */
var i = 2;
console.log("The first value of the variable i is "+i+".");

for (var i = 0; i < 4; i++) {
  console.log(i);
}

console.log("!!!! The final value of the variable i is "+i+", not 3 and also not 2. !!!!");





Discover More
DOM - AppendChild

AppendChild is a node dom tree operation that: adds (a new node) move (a node of the DOM tree) as children at the end of all already existing children of this node. where: returnedValue is:...
DOM - Collection Iteration (NodeList, ..)

in the DOM. API A selection of element with the Web API DOM is returned as a nodelist. For forEach Property Web API Example Source...
DOM - NodeList (Collection)

A nodelist is the result of a selection that returns more than one node (Generally an element) Nodelist: HTMLAllCollection, HTMLFormControlsCollection, HTMLOptionsCollection, interfaces are...
Javascript - (Loop|Iterator)

This page is how to iterate in Javascript (ie the Iterator pattern in Javascript) To iterate / loop in Javascript, you can use the following syntax structure: the for statement the functional...
Javascript - (Object) Key

Object key management. A key is also known as a property name :) Check all properties (also the inherited one) check only the direct properties of the object and not the inherited one...
Javascript Lexical Environment Scope Chaine
Javascript - (Variable) Scope (Namespace)

Variable scope in Javascript. variable scope is delimited by the function definition, not a the block level A block has the scope of its inner function. As a function is also an object, by generalization,...
Javascript - Array

array in Javascript can contain any type of data. Different types of values can be stored within the same array Because arrays are special objects (as typeof implies), they can also have properties, including...
Javascript - Map function (Functional programming)

The functional programming function map in a Javascript context is only an array method as all functional programming javascript method. map With an Arrow function as argument Letters that we...
Javascript - Object

object in javascript An object is a data type in Javascript. An object follows a json data structure with key that holds property value. The value may be of a variable or a function In JavaScript, a...
Javascript - Set

The Set interface represents a set data type and works only with primitive type as there is no way to define an object equality function. object Jsdoc Declaration Add/Delete Size Contains...



Share this page:
Follow us:
Task Runner