导航
×
   ❮   
HTML CSS JavaScript PHP Go Sass W3C Colors ECMS

JS 参考手册

JS 参考手册(字母排序)

JavaScript

JS Arrays JS Boolean JS Classes JS Dates JS Error JS Global JS Iterators JS JSON JS Maps JS Math JS Numbers JS Objects JS Operators JS Assignment JS Arithmetic JS Comparison JS Logical Operators JS Bitwise Operators JS Misc Operators JS Precedence JS Promises JS RegExp Patterns JS RegExp Reference JS Sets JS Statements JS Strings JS Typed Arrays JS Typed Reference

Window

Window Object Window Console Window History Window Location Window Navigator Window Screen

HTML DOM

HTML Documents HTML Elements HTML Attributes HTML Collection HTML NodeList HTML DOMTokenList HTML Styles
alignContent alignItems alignSelf animation animationDelay animationDirection animationDuration animationFillMode animationIterationCount animationName animationTimingFunction animationPlayState background backgroundAttachment backgroundClip backgroundColor backgroundImage backgroundOrigin backgroundPosition backgroundRepeat backgroundSize backfaceVisibility border borderBottom borderBottomColor borderBottomLeftRadius borderBottomRightRadius borderBottomStyle borderBottomWidth borderCollapse borderColor borderImage borderImageOutset borderImageRepeat borderImageSlice borderImageSource borderImageWidth borderLeft borderLeftColor borderLeftStyle borderLeftWidth borderRadius borderRight borderRightColor borderRightStyle borderRightWidth borderSpacing borderStyle borderTop borderTopColor borderTopLeftRadius borderTopRightRadius borderTopStyle borderTopWidth borderWidth bottom boxShadow boxSizing captionSide caretColor clear clip color columnCount columnFill columnGap columnRule columnRuleColor columnRuleStyle columnRuleWidth columns columnSpan columnWidth counterIncrement counterReset cssFloat cursor direction display emptyCells filter flex flexBasis flexDirection flexFlow flexGrow flexShrink flexWrap font fontFamily fontSize fontStyle fontVariant fontWeight fontSizeAdjust height isolation justifyContent left letterSpacing lineHeight listStyle listStyleImage listStylePosition listStyleType margin marginBottom marginLeft marginRight marginTop maxHeight maxWidth minHeight minWidth objectFit objectPosition opacity order orphans outline outlineColor outlineOffset outlineStyle outlineWidth overflow overflowX overflowY padding paddingBottom paddingLeft paddingRight paddingTop pageBreakAfter pageBreakBefore pageBreakInside perspective perspectiveOrigin position quotes resize right scrollBehavior tableLayout tabSize textAlign textAlignLast textDecoration textDecorationColor textDecorationLine textDecorationStyle textIndent textOverflow textShadow textTransform top transform transformOrigin transformStyle transition transitionProperty transitionDuration transitionTimingFunction transitionDelay unicodeBidi userSelect verticalAlign visibility width wordBreak wordSpacing wordWrap widows zIndex

HTML Events

HTML Events HTML Event Objects HTML Event Properties HTML Event Methods

Web APIs

API Canvas API Console API Fetch API Fullscreen API Geolocation API History API MediaQueryList API Storage API Validation API Web

HTML 对象

<a> <abbr> <address> <area> <article> <aside> <audio> <b> <base> <bdo> <blockquote> <body> <br> <button> <canvas> <caption> <cite> <code> <col> <colgroup> <datalist> <dd> <del> <details> <dfn> <dialog> <div> <dl> <dt> <em> <embed> <fieldset> <figcaption> <figure> <footer> <form> <head> <header> <h1> - <h6> <hr> <html> <i> <iframe> <img> <ins> <input> button <input> checkbox <input> color <input> date <input> datetime <input> datetime-local <input> email <input> file <input> hidden <input> image <input> month <input> number <input> password <input> radio <input> range <input> reset <input> search <input> submit <input> text <input> time <input> url <input> week <kbd> <label> <legend> <li> <link> <map> <mark> <menu> <menuitem> <meta> <meter> <nav> <object> <ol> <optgroup> <option> <output> <p> <param> <pre> <progress> <q> <s> <samp> <script> <section> <select> <small> <source> <span> <strong> <sub> <summary> <sup> <table> <tbody> <td> <tfoot> <th> <thead> <tr> <textarea> <time> <title> <track> <u> <ul> <var> <video>

JavaScript while 语句


实例

只要变量 (i) 小于 5,就会循环代码块:

var text = "";
var i = 0;
while (i< 5) {
  text += "<br>The number is " + i;
  i++;
}
亲自试一试 »

页面下方有更多实例。


定义和用法

while 语句创建循环,该循环在指定条件为真时执行。

只要条件为真,循环就会继续运行。只有当条件变为假时它才会停止。

JavaScript 支持不同类型的循环:

  • for - 多次循环代码块
  • for/in - 遍历对象的属性
  • for/of - 循环遍历可迭代对象的值
  • while - 在指定条件为真时循环代码块
  • do/while - 循环一次代码块,然后在指定条件为真时重复循环

提示: 请使用 break 语句跳出循环,使用 continue 语句跳过循环中的某个值。


浏览器支持

Statement
while Yes Yes Yes Yes Yes

语法

while (condition) {
  code block to be executed
}

参数值

参数 描述
condition

必需。定义运行循环(代码块)的条件。如果返回 true,循环将重新开始,如果返回 false,循环将结束。

注释:如果条件始终为真,循环将永远不会结束。这将使您的浏览器崩溃。

注释:如果您使用带有条件的变量,请在循环之前对其初始化,并在循环内将其递增。如果忘记增加变量,循环将永远不会结束。这也会使您的浏览器崩溃。


技术细节

JavaScript 版本: ECMAScript 1

更多实例

实例

循环遍历数组的索引,从 cars 数组中收集汽车名称:

var cars = ["BMW", "Volvo", "Saab", "Ford"];
var text = "";
var i = 0;
while (i< cars.length) {
  text += cars[i] + "<br>";
  i++;
}
亲自试一试 »

实例解析:

  1. 首先,我们在循环开始之前设置一个变量 (var i = 0;)
  2. 然后,我们定义循环运行的条件。只要变量小于数组的长度(即 4),循环就会继续
  3. 每次循环执行时,变量加一 (i++)
  4. 一旦变量不再小于 4(数组的长度),条件为假,循环结束

实例

向后循环数组索引:

var cars = ["BMW", "Volvo", "Saab", "Ford"];
var text = "";
var len = cars.length;
while (len--) {
  text += cars[len] + "<br>";
}
亲自试一试 »

实例

使用 break 语句 - 循环代码块,但当变量 i 等于 "3" 时退出循环:

var text = "";
var i = 0;
while (i< 5) {
  text += "<br>The number is " + i;
  i++;
  if (i == 3) {
    break;
  }
}
亲自试一试 »

实例

使用 continue 语句 - 循环代码块,但跳过 "3" 这个值:

var text = "";
var i = 0;
while (i< 5) {
  i++;
  if (i == 3) {
    continue;
  }
text += "<br>The number is " + i;
}
亲自试一试 »

相关页面

JavaScript 教程: JavaScript While 循环

JavaScript 参考手册: JavaScript do ... while Statement

JavaScript 参考手册: JavaScript for 语句

JavaScript 参考手册: JavaScript break 语句

JavaScript 参考手册: JavaScript continue 语句


Copyright ©2026 蒙ICP备2026009383号 All Rights Reserved 提供的内容仅用于学习和测试,不保证内容的正确性。