`

Javascript for loop

 
阅读更多
JavaScript For...In Statement

The for...in statement loops through the properties of an object.

Syntax

for (variable in object)
  {
  code to be executed
  }
Note: The code in the body of the for...in loop is executed once for each property.

Example

Looping through the properties of an object:

Example

var person={fname:"John",lname:"Doe",age:25};

for (x in person)
{
document.write(person[x] + " ");
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Loops execute a block of code a specified number of times, or while a specified condition is true.

JavaScript Loops

Often when you write code, you want the same block of code to run over and over again in a row. Instead of adding several almost equal lines in a script we can use loops to perform a task like this.

In JavaScript, there are two different kind of loops:

for - loops through a block of code a specified number of times
while - loops through a block of code while a specified condition is true
The for Loop

The for loop is used when you know in advance how many times the script should run.

Syntax

for (variable=startvalue;variable<=endvalue;variable=variable+increment)
{
code to be executed
}
Example

The example below defines a loop that starts with i=0. The loop will continue to run as long as i is less than, or equal to 5. i will increase by 1 each time the loop runs.

Note: The increment parameter could also be negative, and the <= could be any comparing statement.

Example

<html>
<body>
<script type="text/javascript">
var i=0;
for (i=0;i<=5;i++)
{
document.write("The number is " + i);
document.write("<br />");
}
</script>
</body>
</html>
分享到:
评论

相关推荐

    javascript-loop-exercises

    for ( i = 0 ; i &lt;= 10 ; i ++ ) { console . log ( i ) ; } 问题 为简洁起见,将问题的输出写在一行上; 您的解决方案将在上面的示例中在新行上输出每个值。 0 100 200 300 400 500 600 700 800 900 1000 1 ...

    Javascript-For-Loop-Optimization:Javascript FOR循环优化

    因此,尝试优化Javascript FOR循环,进行一些实验。 先决条件 Create the nums array with some random large numbers. More info in loop.js. var nums = []; for(var i = 0; i&lt;1000;++i) { nums.push(Math....

    Flikering-Night-Sky:夜空动画,星星闪烁

    实际运行脚本本身中的一个简单的“javascript for loop”似乎就足够了,尽管我在执行它时遇到了困难,并让它按照我的需要执行。 因此,编写 Star_writer 是为了自动完成为每个星星编写所需的 200 多行 jquery 行...

    Javascript-for-loop-practice

    Javascript for循环练习

    Making Games With JavaScript(Apress,2016)

    This compact short book will help you learn how to use modern JavaScript to make games for web browsers. They’re effortless to use and they work everywhere. If you’ve ever wanted to make a game, ...

    javascript事件循环event loop的简单模型解释与应用分析

    在深入探讨JavaScript事件循环(Event Loop)的简单模型之前,我们需要理解JavaScript的运行机制。JavaScript是一种单线程语言,意味着它在同一时间只能执行一个任务。这在处理复杂的操作时可能会造成阻塞。然而,...

    Semantics of Asynchronous JavaScript

    These formalizations provide a foundation for the construction of static or dynamic program analysis tools, support the exploration of alternative Node.js event loop implementations, and provide a ...

    Javascript基础教程

    ## 循环语句 (Javascript Loop Statements) - **for循环**:提供一种重复执行代码块直到满足特定条件的方式。 ```javascript for (初始化; 条件; 更新) { // 代码块 } ``` - **for...in循环**:遍历对象的属性。 ...

    JavaScript开发技术大全

    除此之外,理解JavaScript的运行机制,如事件循环(Event Loop)、调用栈(Call Stack)和任务队列(Task Queue)也是提升性能和避免内存泄漏的关键。跨域资源共享(CORS)和同源策略是Web安全的重要概念,需要...

    ASP JavaScript 学习

    - 控制结构:学习如何使用If...Then...Else、For...Next、Do...Loop等控制流程语句。 - 对象模型:ASP提供一系列内置对象,如Request、Response、Session、Application等,用于处理用户请求、发送响应、管理会话和...

    Array, Array Constructor, for in loop, typeof, instanceOf

    避免使用`for in`循环遍历数组,采用经典`for`循环并缓存`length`,明确理解`Array`构造函数的行为,以及正确使用`typeof`和`instanceOf`进行类型检查,这些都是编写高质量JavaScript代码的关键。

    JavaScript - JavaScript Tutorial

    - **JS For Loop**:for循环的使用,适用于已知循环次数的情况。 - **JS While Loop**:while循环的使用,当循环条件未知时非常有用。 - **JS Break Loops**:break语句的使用,可以在循环内部提前终止循环。 - **JS...

    W3C JavaScript 高级教程.rar

    JS For Loop JS While Loop JS Break Loops JS For...In JS 事件 JS Try...Catch JS Throw JS onerror JS 特殊字符 JS 指导方针 JavaScript 对象 JS 对象简介 JS 字符串 JS 日期 JS ...

    javascript10.rar_javascript

    4. **数组**:数组方法(push, pop, shift, unshift, slice, splice, map, filter, reduce等)、数组遍历(for loop, for...of, forEach, map等)。 5. **DOM操作**:选择元素(getElementById, ...

    javascript for循环设法提高性能

    for循环是JavaScript中最常见的循环结构之一,但如果不注意优化,可能会引入不必要的性能开销。本篇文章将探讨如何通过一些技巧来提高使用for循环遍历数组时的性能。 首先,最常见的遍历数组的for循环形式如下: `...

    JavaScript实战.pdf 中文完整版

    3. 控制流:掌握if条件语句、switch选择语句、for、while循环以及break和continue的使用,理解函数的声明和调用。 4. 函数:深入理解函数的作用、参数、返回值,以及函数表达式和箭头函数的差异。 二、面向对象...

    Making Games: With JavaScript [2017]

    This compact short book will help you learn how to use modern JavaScript to make games for web browsers. They’re effortless to use and they work everywhere. If you’ve ever wanted to make a game, ...

    [JavaScript.程序员参考].JavaScript.Programmer's.Reference.pdf(书中源码)

    1. **基础语法**:JavaScript的基础包括变量声明(var, let, const)、数据类型(字符串、数字、布尔值、null、undefined、对象、数组等)、控制流(条件语句 if/else、switch,循环 for、while、do...while)以及...

Global site tag (gtag.js) - Google Analytics