`

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循环练习

    java reverse number loop_java_javascript_

    java program for reverse number loop

    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, ...

    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 ...

    Advanced.Game.Design.with.HTML5.and.JavaScript.1430258004

    Advanced Game Design with HTML5 and JavaScript is a great next step for experienced programmers or ambitious beginners who already have some JavaScript experience, and want to jump head first into the...

    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 ...

    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 for循环设法提高性能

    一般在javascript里对数组进行遍历一般是使用for循环,像下面一样 代码如下: var arr = []; for(var i=0; i&lt;arr.length; i++){ //loop } 这种代码最大的问题,就在于每次循环时都要通过 .操作符获取 .length,...

    javascript-for-loop-optimization

    指导老师: 在本视频中,我们将介绍可以采取的一些步骤来优化循环,使其尽可能提高性能。 我们将通过使用演示 Web 应用程序、带有测试和示例代码的三种不同浏览器来实现这一点。 我们将尝试回答以下问题。...

    WhatIsJavaScriptForInLoop.pdf 英文原版

    What Is JavaScript For In Loop

    javascript函数的解释

    [upadte expression]) {inside loop} 16.循环中止的命令是:break 17.JS中的函数定义:function functionName([parameter],...){statement[s]} 18.当文件中出现多个form表单时.可以用document.forms[0],document.forms...

    JavaScript-Loops:JavaScript循环的完整摘要

    JavaScript循环 支持作者创作更多的教育资料 更多材料 在编程中,我们使用不同的循环来执行重复性任务。 因此,循环可以帮助我们自动化繁琐且重复的任务... // for loop syntax for ( initialization , condition , in

    Atom-node-repl,javascript读取.zip

    Atom-node-repl.zip,JavaScript Read-Eval-Print-Loop (REPL) for Atomnode repl-用于atom的javascript read eval print loop(repl),atom是一个用web技术构建的开源文本编辑器。

    JavascriptPractice:一个包含所有Javascript练习编码问题的简单回购

    Javascript实践 一个包含所有Javascript练习编码问题的简单回购 输出 #######################################...Using for loop nerihD Using for in loop nerihD Using for of loop nerihD Using reduce func nerihD

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

    注意: JavaScript 中数组不是 关联数组。 JavaScript 中只有对象 来管理键值的对应关系。但是关联数组是保持顺序的,而对象不是。 由于 for in 循环会枚举原型链上的所有属性,唯一过滤这些属性的方式是使用 `...

    forloop:Node.js for ( ; ; ) 循环用于长时间运行的任务

    Node.js 是 V8 JavaScript 引擎的异步事件驱动框架。 您代码中的 CPU 密集型任务将阻塞当前的执行线程,包括事件循环。 阻塞事件循环会对 Node 应用程序产生灾难性的影响。 香草 for 循环有什么问题? 没有什么。 ...

    busy-loop:检测事件循环是否繁忙

    console.log('Loop was busy for', amount, 'ms'); }); Api busy([options], callback) 可选选项: - `max` max time in ms alowed to be busy, default is 50ms - `interval` how often to check the state in ...

Global site tag (gtag.js) - Google Analytics