皮肤爱出油是什么原因| 吃什么补血贫血| 核黄素是什么| 佳人是什么生肖| 子宫肌瘤有什么危害| 肠胃不好适合喝什么茶| 喝咖啡有什么好处和坏处| robinhood是什么牌子| 树挪死人挪活是什么意思| 口腔溃疡吃什么药最好| 眼睛为什么会得结膜炎| 西米露是什么材料做的| 女人漏尿是什么原因| 雌二醇高说明什么原因| 肩周炎口服什么药最好| 西安有什么美食| 被交警开罚单不交有什么后果| 雷锋是什么生肖| 10月份是什么星座的| 扁桃体发炎严重吃什么药好得快| 阴道炎用什么药| 芥花油是什么油| 小儿安现在叫什么名| 赤者念什么| 白喉是什么病| 连长相当于地方什么官| 女儿是小棉袄儿子是什么| 受精卵着床是什么意思| 灵魂伴侣是指什么意思| 30岁属什么的生肖| 肉桂茶适合什么人喝| 什么为力| 凯莉包是什么牌子| 属马的本命佛是什么佛| 霸王龙吃什么| 谌读什么| 市斤是什么意思| eligible是什么意思| 风寒感冒吃什么水果| 朝三暮四是什么生肖| 榨菜的原料菜叫什么| 朔字五行属什么| 颈椎压迫手麻吃什么药| 一带一路指的是什么| 肺炎吃什么药效果好| 蚕蛾吃什么| 左侧头疼是什么原因引起的| 日有所思夜有所梦是什么意思| 17点到19点是什么时辰| 阴毛瘙痒是什么原因| 丁什么丁什么成语| 藏青色配什么颜色好看| 隶属什么意思| 什么啤酒最好喝| ct是什么| 覅什么意思| 一的五行属性是什么| 什么样的油菜花| 亚健康是什么意思| 中国的四大发明是什么| 拜复乐是什么药| 维生素b2有什么功效| 子宫肌瘤吃什么药| 巴基斯坦是什么人种| 子宫什么样子图片| 牛跟什么生肖相冲| 小巫见大巫是什么意思| 为什么有的人特别招蚊子| 双子男喜欢什么样的女生| 人中发红是什么原因| 甲沟炎涂抹什么药膏最有效| 血糖高是什么引起的| 什么叫台风| 有口臭去医院挂什么科| 生吃西红柿有什么好处和坏处| 什么邮箱最好用最安全| 掉头发是因为什么| 手术后为什么要平躺6小时| simon是什么意思| 玄孙是什么意思| 什么是大小周| 头不由自主的摇晃是什么病| 南无是什么意思| cnb是什么意思| 没有料酒用什么去腥| 闰月鞋买什么颜色| 戾气太重是什么意思| 酩酊是什么意思| 什么是龟头炎| 两三分钟就射什么原因| od什么意思| 小儿拉肚子吃什么药好得快| 精神障碍是什么病| 不显山不露水是什么意思| 升结肠憩室是什么意思| 玫琳凯属于什么档次| 玉的主要成分是什么| 单身领养孩子需要什么条件| 全身体检挂什么科| 月经推迟半个月是什么原因| 712什么星座| 把握时机是指什么生肖| 确幸是什么意思| 赛博朋克什么意思| 头发软是什么原因| 骨龄大于年龄意味着什么| 为什么拼音| 帕金森是什么引起的| 星期三打喷嚏代表什么| 腹泻吃什么药好| 艾滋病通过什么传播| 紫茉莉什么时候开花| 路旁土命什么意思| 双侧肋膈角锐利是什么意思| 5月12号是什么星座| 心胸狭窄是什么意思| 世界上有什么| 吃素对身体有什么好处| 涅盘是什么意思| 连襟是什么关系| 总是抽筋是什么原因| 吃粽子是什么节日| 俄罗斯乌克兰为什么打仗| 吃维生素b有什么好处| 幽门螺旋杆菌感染是什么意思| 马驹是什么意思| 一什么一什么| 吃什么盐比较好有利于健康| 工作是为了什么| 一岁半宝宝反复发烧是什么原因| 桂圆补什么| 龟兔赛跑的故事告诉我们什么道理| 复出是什么意思| 血糖高能吃什么主食| 大脑记忆力下降是什么原因| 女人做春梦预示着什么| 毓字五行属什么| 炒面用什么面条最好| 打耳洞后不能吃什么| 海里有什么鱼| 肚脐上面疼是什么原因| 为什么白带是绿色| 没有力气是什么原因| 7月1日是什么星座| 蚊香对人体有什么危害| 赞什么不已| 十一点半是什么时辰| 雷蒙欣氨麻美敏片是什么药| 积劳成疾的疾是什么意思| 火气重喝什么茶| 牙疼吃什么饭菜比较好| 晚上放屁多是什么原因| tsh代表什么| 间质性肺病是什么意思| 刘璋和刘备什么关系| 即使什么也什么| xl什么牌子| 为什么会有湿疹| 地果是什么| 医学生规培是什么意思| 扁桃体发炎吃什么中成药| 怀孕了不能吃什么| 篇幅是什么意思| 十一月八号是什么星座| 碳酸盐是什么| 吃什么降血糖快| 石榴木命是什么意思| 花哨是什么意思| 全血低切相对指数偏高什么意思| 一到晚上就饿什么原因| 什么是胸推| 吃甲硝唑有什么副作用| 依云矿泉水为什么贵| 面包用什么面粉| oo什么意思| 什么是值机| 脱相是什么意思| mopar是什么牌子| 梦见监狱是什么意思| 人类免疫缺陷病毒抗体是什么意思| 眼袋是什么原因造成的| 今天天气适合穿什么衣服| 1975年属兔五行属什么| 包二奶什么意思| 补体c1q偏低说明什么| 白醋泡脚有什么功效| 鼻尖出汗是什么原因| 中国属于什么人种| 水逆是什么意思| 鸡是什么动物| nt 是什么检查| 小螃蟹吃什么食物| 化验血常规能查出什么| 1924年属什么| 挫是什么意思| 黑松露什么味道| 药品经营与管理学什么| 闰月鞋买什么颜色| 灰枣与红枣有什么区别| 血小板低会引发什么病| 硼酸是什么| 点痣挂什么科室| 锌是什么颜色| 阑尾炎吃什么食物好| 5月9日是什么星座| 局灶癌变是什么意思| 故的偏旁是什么| 属鸡的适合干什么行业最赚钱| 半路杀出个程咬金是什么意思| 什么车不能开| 什么是肺腺癌| 王安石是什么朝代的| 蒲公英长什么样| 追悔莫及什么意思| 阿普唑仑是什么药| 白塞氏病是什么病| 心脏彩超可以检查什么| 紧急避孕药有什么副作用| 什么好像什么| m代表什么单位| 是的是什么意思| 看走眼是什么意思| 椎间盘膨出是什么意思| 家里出现蚂蚁预示什么| 聊胜于无的聊是什么意思| std什么意思| 大运是什么| 星标朋友是什么意思| 洗涤剂是什么| 怀孕腿抽筋是因为什么原因引起的| 小三阳吃什么药| 水杯什么材质好| 闭合性跌打损伤是什么意思| 赢荡为什么传位嬴稷| aba是什么意思| 哲五行属什么| 外科看什么病| 胃动力不足吃什么药| 胃潴留是什么意思| 七月十一日是什么日子| 尿酸低吃什么| 什么猫好看| 什么是虫草| 心功能不全是什么意思| 喝酒后呕吐是什么原因| 50岁今年属什么生肖| 梦到镯子碎了什么预兆| 幽门螺旋杆菌是什么病| 米杏色是什么颜色| 飞机什么时候停止登机| 琪五行属性是什么| 咽喉炎有什么症状| 防晒衣什么品牌好| 睡眠瘫痪症是什么| 胃糜烂吃什么药最好| 盲约大结局是什么| 咽后壁淋巴滤泡增生吃什么药| 2022年是属什么| cp是什么的缩写| 福荫是什么意思| 眼痒痒是什么原因引起| 18罗汉都叫什么名字| 江小白加雪碧什么意思| 5岁属什么生肖| 百度

白乾笙五姨太黄采薇和二爷的结局 结局怎么死的

百度 圆满完成了“7·02”晴隆天然气管道泄漏爆炸处置、“8·28”毕节纳雍山体崩塌救援等急难险重任务,得到了省委、省政府和人民群众的高度肯定。

In JavaScript, the for loop is used for iterating over a block of code a certain number of times, or to iterate over the elements of an array.

Here's a quick example of the for loop. You can read the rest of the tutorial for more details.

Example

for (let i = 0; i < 3; i++) {
    console.log("Hello, world!");
}

// Output:
// Hello, world!
// Hello, world!
// Hello, world!

In this example, we used the for loop to print "Hello, world!" three times to the console.


JavaScript for loop Syntax

The syntax of the for loop is:

for (initialExpression; condition; updateExpression) {
    // for loop body
}

Here,

  • initialExpression - Initializes a counter variable.
  • condition - The condition to be evaluated. If true, the body of the for loop is executed.
  • updateExpression - Updates the value of initialExpression.

Once an iteration of the loop is completed, the condition is evaluated again. The process continues until the condition is false.

To learn more about the condition, visit JavaScript Comparison and Logical Operators.


Flowchart of JavaScript for Loop

Working of for loop in JavaScript with flowchart
Flowchart of JavaScript for loop

Example 1: Print Numbers From 1 to 5

for (let i = 1; i < 6; i++) {
    console.log(i);
}

Output

1
2
3
4
5

In this example, we have printed numbers from 1 to 5 using a for loop.

Here is how this program works:

Iteration Variable Condition: i < 6 Action
1st i = 1 true 1 is printed.
i is increased to 2.
2nd i = 2
true 2 is printed.
i is increased to 3.
3rd i = 3 true 3 is printed.
i is increased to 4.
4th i = 4
true 4 is printed.
i is increased to 5.
5th i = 5
true 5 is printed.
i is increased to 6.
6th i = 6
false The loop is terminated.

Example 2: Display Sum of n Natural Numbers

// program to display the sum of natural numbers

let sum = 0;
const n = 100

// loop from i = 1 to i = n // in each iteration, i is increased by 1 for (let i = 1; i <= n; i++) { sum += i; // sum = sum + i }
console.log(`sum: ${sum}`); // Output: sum: 5050

Initially, the value of sum is 0, while n has a constant value of 100.

Then, we iterate a for loop from i = 1 to n. In each iteration,

  • i is added to sum.
  • Then, the value of i is increased by 1.

When i becomes 101, the test condition becomes false and sum will be equal to 0 + 1 + 2 + ... + 100.


Iterate Through an Array

A for loop can also be used to iterate over elements of an array. For example,

const fruits = ["apple", "banana", "cherry"];

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

Output

apple
banana
cherry

This loop iterates through the fruits array and prints each element to the console.


More on JavaScript for loop

Nested for Loops

A for loop can also have another for loop inside it. For each cycle of the outer loop, the inner loop completes its entire sequence of iterations. For example,

// outer loop 
for (let i = 0; i < 3; i++) {

    // inner loop
    for (let j = 0; j < 2; j++) {
        console.log(`i = ${i}, j = ${j}`);
    }

}

Output

i = 0, j = 0
i = 0, j = 1
i = 1, j = 0
i = 1, j = 1
i = 2, j = 0
i = 2, j = 1

Here,

  • Outer Loop - Runs from i = 0 to 2.
  • Inner Loop - Runs from j = 0 to 1.

In each iteration of the outer loop, the inner loop runs from j = 0 to 1.

JavaScript Infinite for loop

In JavaScript, we can create an infinite for loop by setting a condition that always evaluates to true. For example,

for (let i = 0; true; i++) {
    console.log("This loop will run forever!");
}

Output

This loop will run forever!
This loop will run forever!
This loop will run forever!
…

In this example, the condition in the for loop is explicitly set to true.

Since this condition never changes and always evaluates to true, the loop will continue indefinitely (until the memory is full).

Note: Creating an infinite loop is something that we should generally avoid, as it will consume all available resources to execute the loop endlessly.

Omitting Parts of the for Loop

We can omit any part of the for loop declaration and include it in a different part of the code.

Let's look at an example.

// initialization outside the loop
let i = 0;

// omit initialization and update statements
for (; i < 3; ) {

    console.log(`i is ${i}`);

    // increment inside the loop body
    i++; 
}

Output

i is 0
i is 1
i is 2

Here, we have initialized i before the loop, which iterates as long as i is less than 3.

Notice that i is incremented within the loop body, which allows us to skip the update statement inside the parentheses () of the for loop.

In other words, for (; i < 3; ) indicates omitted initialization and update expression, focusing only on the condition.


Also Read:

Before we wrap up, let’s put your knowledge of JavaScript for loop to the test! Can you solve the following challenge?

Challenge:

Write a function to calculate the factorial of a number.

  • The factorial of a non-negative integer n is the product of all positive integers less than or equal to n.
  • For example, the factorial of 3 is 3 * 2 * 1 = 6.
  • Return the factorial of the input number num

Video: JavaScript for Loop

Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges
诸事顺遂是什么意思 腹水是什么 酸性体质是什么意思 奶茶喝多了有什么危害 腱鞘炎有什么症状
一般是什么意思 印度人口什么时候超过中国 亨廷顿舞蹈症是什么病 舌面有裂纹是什么原因 红霉素软膏治什么病
量是什么意思 jm是什么 美国为什么打越南 手比脸白是什么原因 什么只好什么
口腔溃疡吃什么水果好得快 茶叶渣属于什么垃圾 学前班是什么意思 gl小说是什么意思 冠状沟溃疡是什么病
吃什么最补胶原蛋白hcv8jop8ns1r.cn 正月是什么意思hcv7jop5ns1r.cn 梦到开车是什么意思hcv9jop1ns4r.cn 锻练与锻炼有什么区别baiqunet.com 广州立冬吃什么travellingsim.com
红豆杉是什么植物hcv9jop2ns3r.cn 二阴指的是什么clwhiglsz.com 壬午五行属什么mmeoe.com 胃寒胃凉吃什么药hcv9jop1ns2r.cn 乙肝病毒表面抗原阳性是什么意思hcv7jop6ns5r.cn
结核t细胞阳性说明什么beikeqingting.com 06是什么生肖gangsutong.com 826是什么意思zsyouku.com 怀孕了吃什么hcv8jop4ns5r.cn 中暑了吃什么好tiangongnft.com
什么颜色衣服显皮肤白hcv9jop0ns0r.cn 东京热是什么hcv7jop9ns5r.cn 84消毒液不能和什么一起用zhiyanzhang.com 什么清肠茶好hcv9jop6ns4r.cn 就是什么意思hcv8jop6ns9r.cn
百度