A detailed introduction to for/of, for/in in JavaScript

A detailed introduction to for/of, for/in in JavaScript

In JavaScript , there are several common ways to write for loops:

The first and most common way of writing:

nums = [1,2,3,4]

for (let i=0; i<nums.length; i++){
    console.log(nums[i])
}

Second type:

The second for/of syntax is available since ES6 . It can directly iterate each element in the array without getting the element by subscript index position. In fact, as long as it is an iterable object, for/of can be used.

for (let item of nums){
    console.log(item)
}

The third type:

The third writing method for/in . Unlike for/of must be iterable objects, for/in can iterate over any object. The name of the property of the loop iteration object. If it is an array, the iterated value is the subscript index of the array, which is the same as the original for .

let p = {name:"zhang", age:10}

for(let key in p){
    console.log(p[key])
}

Output:

zhang
10

for (let index in nums){
    console.log(nums[index])
}


for/in cannot enumerate all the properties of the iterated object, such as symbol properties cannot be enumerated

When defining variables for/of and for/in syntax, you can also use the const keyword. const declares a constant value during a loop iteration.

This is the end of this article about the detailed introduction of for/of, for/in in JavaScript. For more relevant JavaScript for/of, for/in introduction content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Differences and usage examples of for, for...in, for...of and forEach in JS
  • Summary of the use of three for loop statements in JavaScript (for, for...in, for...of)
  • Analyzing the difference between JavaScript for in and for of through examples
  • Summary of examples of using forEach, for in, and for of loops in js
  • Detailed explanation of js traversal (forEach, map, for, for...in, for...of)
  • Detailed explanation of the usage of for...in and for...of in Js
  • A brief analysis of the usage of map, filter, some, every, forEach, for in, for of in JS
  • A comprehensive analysis of the loop methods in JavaScript: forEach, for-in, for-of

<<:  Detailed tutorial on installation and configuration of compressed version of MySQL database

>>:  CSS realizes the mask effect when the mouse moves to the image

Recommend

Let’s take a look at JavaScript precompilation (summary)

JS running trilogy js running code is divided int...

Ubuntu Basic Tutorial: apt-get Command

Preface The apt-get command is a package manageme...

Vue+element+oss realizes front-end fragment upload and breakpoint resume

Pure front-end implementation:切片上傳斷點續傳.斷點續傳needs ...

idea combines docker to realize image packaging and one-click deployment

1. Install Docker on the server yum install docke...

Example code for implementing triangles and arrows through CSS borders

1. CSS Box Model The box includes: margin, border...

JavaScript to display hidden form text

This article shares the specific code of JavaScri...

JavaScript implementation of the Game of Life

Table of contents Concept Introduction Logical ru...

Interpretation of 17 advertising effectiveness measures

1. 85% of ads go unread <br />Interpretatio...

Using js to achieve the effect of carousel

Today, let's talk about how to use js to achi...

Pessimistic locking and optimistic locking in MySQL

In relational databases, pessimistic locking and ...