Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the Loop in JavaScript

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "how to achieve the cycle of JavaScript", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to achieve the cycle of JavaScript" this article.

Cycle 1.for cycle

For is the most commonly used loop, mainly used to loop arrays

Let arr = [1, 2, 3]; for (let item0; I {arrCopy1.push (item * 2)}) console.log (arrCopy1) / / [2,4,6]

ForEach () executes the callback function once for each array element

Items that have been deleted or uninitialized will be skipped (for example, on sparse arrays)

Unlike map () and reduce (), it does not return a value, it always returns undefind.

ForEach () has no way to stop or jump out of the forEach () loop except to throw an exception.

3.while

The while statement can loop through a specified piece of code on the premise that a conditional expression is true until the loop ends when that expression is not true.

Example:

Let n = 0 expected output while while (n < 3) {natively created;} console.log (n); / / expected output: 3

Note: use the break statement to stop the loop before the condition evaluation result is true

4.do... While

Do... The while statement creates a loop that executes the specified statement until the condition value is false. Condition is detected after statement is executed, so the specified statement is executed at least once

Example:

Const list = ['averse,' baked,'c'] let I = 0do {console.log (list [I]) / / value console.log (I) / / index I = I + 1} while (I < list.length) 5.for... In

For... The in loop can be used to traverse the list of enumerable properties of an object (including the [[Prototype]] chain)

It is mainly used to traverse objects, and attribute values can be obtained through the attribute list.

For (let property in object) {console.log (property) / / property name console.log (objectproperty) / / property value} above is all the content of the article "how to achieve a loop in JavaScript". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report