首页 > 编程语言 >JavaScript中的循环(6个)

JavaScript中的循环(6个)

时间:2023-06-02 10:34:30浏览次数:34  
标签:count const log JavaScript while 循环 console loop

Loopes

  • for
  • while
  • do while
  • for of
  • forEach
  • for in

1. for

for(initialization, condition, increment/decrement) {
  // code goes here
}

eg: 
for(let i  = 0; i < 6; i++) {
  console.log(i)
}

2. while

Using the while loop when we do not know how man iteration we go in advance.

let count = prompt('Enter a positive number: ');
white(count > 0){
  console.log(count);
  count--

3. do while

Do while run at leat once if the condition is true or false;

let count = 0;
do {
  console.log(count)
  count++
} while (count < 11)

4. for of

The for of loop is very handy to use it with array.
If we are not interested in the index of the array
a for of loop is preferable to regular for loop or forEach loop.

const numbers = [1, 2, 3, 4, 5]
for(const number of numbers) {
  console.log(number);
}

const countries = ['Finland', 'Sweden', 'Norway', 'Denmark', 'Iceland']
for (const country of countries) {
  console.log(country.toUpperCase())
}

5. forEach

If we are interested in the index of the array
forEach is preferable to for of loop. The forEach array method takes a callback function,
the callback function takes three arguments; the item, index and the array itself.

const countries = ['Finland', 'Sweden', 'Norway', 'Denmark', 'Iceland']
countries.forEach((country, i, arr) => {
  console.log(i, country.toUpperCase())
})

6. for in

The for in loop can be used with object literals to get the keys of the object.

const user = {
  firstName: 'ZZ',
  lastName: 'wang',
  age: 250,
  country: 'Finland',
  skills: ['HTML', 'CSS', 'JS', 'React', 'Node', 'Python', 'D3.js'],
}
for (const key in user) {
  console.log(key, user[key])
}

标签:count,const,log,JavaScript,while,循环,console,loop
From: https://www.cnblogs.com/openmind-ink/p/17451050.html

相关文章

  • 九、python循环语句
    九、python循环语句1.for循环for循环遍历一个对象(比如数据序列,字符串,列表,元组等),根据遍历的个数来确定循环次数。for循环可以看作为定循环,while循环可以看作为不定循环。for循环的基本格式for变量in数据:重复执行的代码foriin(1,2,3,4,5): #这里用小括号表示是......
  • 洛谷P1587 [NOI2016] 循环之美
    原文链接:[sol]P1587[NOI2016]循环之美-icyM3tra'sBlog此为备份。题目传送门0.前言提供一种式子较为简洁且不算难写的做法。以及介绍一种较为优秀的dp版杜教筛实现。1.式子推导我们知道纯循环小数一定可以写成分母形如\(k^a-1\)的分数。因此约分后的分母一定......
  • JavaScript 基础知识总结
    概述JavaScript基础分为三个部分:ECMAScript:JavaScript的语法标准。包括变量、表达式、运算符、函数、if语句、for语句等。DOM:DocumentObjectModel(文档对象模型),操作页面上的元素的API。比如让盒子移动、变色、改变大小、轮播图等等。BOM:BrowserObjectModel(浏览器对象模型),操......
  • javascript设计模式-责任链
    责任链可以用来消除请求的发送者和接收者之间的耦合,这是通过实现一个由隐式地对请求进行处理的对象组成的链而做到的。链中的每个对象可以处理请求,也可以将其传给下一个对象。JS内部就使用了这种模式来处一事件捕获和冒泡问题。一般的结构如下:发送者知道链中的第一个接收者,它向这个......
  • Python循环语句
    #循环语句#使用while循环打印出0-100的所有数字#循环的初始化条件num=1#当num<100时,会一直执行循环体whilenum<101:  print("num=",num)  #迭代语句  num+=1print("循环结束")print("---------------------------")#使用while遍历字符串str="TheageofM......
  • linux for循环
    目录一、单层for二、双层for三、实验            一、单层for    格式:      1.第一种格式foriin变量($(cat/1.txt)或者{1..10})      dodone2.第二种格式for((i......
  • Collection集合的遍历方式二:增强for循环
       ......
  • C++ 循环
     有的时候,可能需要多次执行同一块代码。一般情况下,语句是顺序执行的:函数中的第一个语句先执行,接着是第二个语句,依此类推。编程语言提供了允许更为复杂的执行路径的多种控制结构。循环语句允许我们多次执行一个语句或语句组,下面是大多数编程语言中循环语句的一般形式:https:/......
  • z-index控制层级显示【JavaScript-Dom&Bom】
    溢出设置overflowvisible(默认)超出部分显示hidden超出部分隐藏scroll超出部分滚动显示行内元素垂直对齐方式vertical-alignbaseline基线对齐(默认)top上对齐middle中间对齐bottom下对齐控制显示层级当元素为非static定位时,可能出现层叠......
  • JS垃圾回收——和其他语言一样,JavaScript 的 GC 策略也无法避免一个问题:GC 时,停止响应
    JavaScript内存管理&垃圾回收机制标记清除js中最常用的垃圾回收方式就是标记清除。当变量进入环境时,例如,在函数中声明一个变量,就将这个而变量标记为“进入环境”。从逻辑上讲,永远不能释放进入环境的变量所占用的内存,因为只要执行流进入相应的环境,就可能会用到它们。而当变量离......