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

Web page HTML code: production of scrolling text

In this section, the author describes the special...

HTML+CSS to implement the sample code of the navigation bar drop-down menu

Effect The pictures in the code can be changed by...

Ubuntu 16.04 mysql5.7.17 open remote port 3306

Enable remote access to MySQL By default, MySQL u...

Vue implements user login and token verification

In the case of complete separation of the front-e...

MySQL executes commands for external sql script files

Table of contents 1. Create a sql script file con...

Example code for implementing anti-shake in Vue

Anti-shake: Prevent repeated clicks from triggeri...

Mysql implements three functions for field splicing

When exporting data to operations, it is inevitab...

How to open MySQL binlog log

binlog is a binary log file, which records all my...

13 JavaScript one-liners that will make you look like an expert

Table of contents 1. Get a random Boolean value (...

Detailed explanation of HTML document types

Mine is: <!DOCTYPE html> Blog Garden: <!...

Example method of deploying react project on nginx

Test project: react-demo Clone your react-demo pr...

View the port number occupied by the process in Linux

For Linux system administrators, it is crucial to...

Javascript closure usage scenario principle detailed

Table of contents 1. Closure 2. Closure usage sce...