Monday, June 26, 2023

JavaScript For of Loop

 

JavaScript For Of

The For Of Loop

The JavaScript for of statement loops through the values of an iterable object.

It lets you loop over iterable data structures such as Arrays, Strings, Maps, NodeLists, and more:

Syntax

for (variable of iterable) {
  // code block to be executed
}

variable - For every iteration the value of the next property is assigned to the variable. Variable can be declared with constlet, or var.

iterable - An object that has iterable properties.


Looping over an Array


Example

const cars = ["BMW""Volvo""Mini"];

let text = "";
for (let x of cars) {
  text += x;
}

Looping over a String

let language = "JavaScript";

let text = "";
for (let x of language) {
text += x;
}


No comments:

Post a Comment

JavaScript Animated

  JavaScript Animated <!DOCTYPE html> <html> <style> #myContainer {   width: 400px;   height: 400px;   position: relative;...