How many types of loops are there in javascript?

PHPz
Release: 2023-05-10 09:49:36
Original
531 people have browsed it

JavaScript is a scripting language often used in web development and other applications. There are many kinds of loops in JS that are used to repeatedly execute a piece of code. This article will introduce loops in JavaScript in detail.

In JavaScript, the following types of commonly used loops are:

  1. for loop
    The for loop is an iterative control structure that can traverse all the items in the array over and over again. element. Its syntax is as follows:
for (initialization; condition; increment) { // code to be executed }
Copy after login

Among them,initializationis the starting value of the loop, which can be a variable or a constant;conditionis the loop condition, When the condition is false, the loop ends;incrementis the change value after each execution of the loop, similar to self-increment or self-decrement. The sample code is as follows:

for (let i = 0; i < 10; i++) { console.log(i); }
Copy after login
  1. while loop

The while loop is another iterative control structure that executes a block of code over and over again as long as a condition is true. Its syntax is as follows:

while (condition) { // code to be executed }
Copy after login

The sample code is as follows:

let i = 0; while (i < 10) { console.log(i); i++; }
Copy after login
  1. do-while loop

The do-while loop is similar to the while loop, but different The difference is that the conditional check is executed after the code block has been executed. Even if the condition is false initially, it will be executed at least once. Its syntax is as follows:

do { // code to be executed } while (condition);
Copy after login

The sample code is as follows:

let i = 0; do { console.log(i); i++; } while (i < 10);
Copy after login
  1. for-in loop

for-in loop is used to traverse objects or arrays attributes or elements. Its syntax is as follows:

for (variable in object) { // code to be executed }
Copy after login

Among them,variableis the name of the variable to be iterated, andobjectis the object to be iterated. The sample code is as follows:

const myObj = { a: 1, b: 2, c: 3 }; for (const property in myObj) { console.log(property + ": " + myObj[property]); }
Copy after login
  1. for-of loop

for-of loop is used to traverse iterable objects, such as arrays and strings. Its syntax is as follows:

for (variable of iterable) { // code to be executed }
Copy after login

Among them,variableis the variable name to be iterated, anditerableis the object to be iterated. The sample code is as follows:

const myArray = [1, 2, 3]; for (const element of myArray) { console.log(element); }
Copy after login

The above are the commonly used loops in JavaScript. Each loop has its own applicable scenarios. When writing JavaScript code, you need to choose different loop structures according to different needs. I hope this article is helpful for everyone to understand JavaScript loops.

The above is the detailed content of How many types of loops are there in javascript?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!