Dòng mới trong vòng lặp javascript

Ký tự xuống dòng (

<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
75) được gọi là chuỗi thoát và nó buộc con trỏ thay đổi vị trí của nó về đầu dòng tiếp theo trên màn hình. Điều này dẫn đến một dòng mới

Vòng lặp cung cấp một cách nhanh chóng và dễ dàng để làm điều gì đó lặp đi lặp lại. Chương này của Hướng dẫn JavaScript giới thiệu các câu lệnh lặp khác nhau có sẵn cho JavaScript

Bạn có thể coi vòng lặp là một phiên bản vi tính hóa của trò chơi trong đó bạn yêu cầu ai đó thực hiện X bước theo một hướng, sau đó Y bước theo hướng khác. Ví dụ: ý tưởng "Đi năm bước về phía đông" có thể được diễn đạt theo cách này dưới dạng một vòng lặp

for (let step = 0; step < 5; step++) {
  // Runs 5 times, with values of step 0 through 4.
  console.log('Walking east one step');
}

Có nhiều loại vòng lặp khác nhau, nhưng về cơ bản chúng đều làm cùng một việc. họ lặp lại một hành động một số lần. (Lưu ý rằng có thể số đó có thể bằng không. )

Các cơ chế vòng lặp khác nhau cung cấp các cách khác nhau để xác định điểm bắt đầu và điểm kết thúc của vòng lặp. Có nhiều tình huống dễ dàng được phục vụ bởi một loại vòng lặp hơn các loại vòng lặp khác

Các câu lệnh cho các vòng lặp được cung cấp trong JavaScript là

  • cho tuyên bố
  • làm. câu lệnh while
  • câu lệnh while
  • tuyên bố có nhãn
  • tuyên bố phá vỡ
  • tiếp tục tuyên bố
  • vì. trong tuyên bố
  • vì. tuyên bố

cho tuyên bố

Vòng lặp

<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
3 lặp lại cho đến khi một điều kiện cụ thể đánh giá là sai. Vòng lặp JavaScript
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
3 tương tự như vòng lặp Java và C
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
3

Một câu lệnh

<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
3 trông như sau

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement

Khi một vòng lặp

<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
3 thực thi, điều sau xảy ra

  1. Biểu thức khởi tạo
    <form name="selectForm">
      <label for="musicTypes">Choose some music types, then click the button below:label>
      <select id="musicTypes" name="musicTypes" multiple>
        <option selected>R&Boption>
        <option>Jazzoption>
        <option>Bluesoption>
        <option>New Ageoption>
        <option>Classicaloption>
        <option>Operaoption>
      select>
      <button id="btn" type="button">How many are selected?button>
    form>
    
    8, nếu có, được thực thi. Biểu thức này thường khởi tạo một hoặc nhiều bộ đếm vòng lặp, nhưng cú pháp cho phép biểu thức ở bất kỳ mức độ phức tạp nào. Biểu thức này cũng có thể khai báo biến
  2. Biểu thức
    <form name="selectForm">
      <label for="musicTypes">Choose some music types, then click the button below:label>
      <select id="musicTypes" name="musicTypes" multiple>
        <option selected>R&Boption>
        <option>Jazzoption>
        <option>Bluesoption>
        <option>New Ageoption>
        <option>Classicaloption>
        <option>Operaoption>
      select>
      <button id="btn" type="button">How many are selected?button>
    form>
    
    9 được đánh giá. Nếu giá trị của
    <form name="selectForm">
      <label for="musicTypes">Choose some music types, then click the button below:label>
      <select id="musicTypes" name="musicTypes" multiple>
        <option selected>R&Boption>
        <option>Jazzoption>
        <option>Bluesoption>
        <option>New Ageoption>
        <option>Classicaloption>
        <option>Operaoption>
      select>
      <button id="btn" type="button">How many are selected?button>
    form>
    
    9 là true, các câu lệnh lặp sẽ thực hiện. Nếu không, vòng lặp
    <form name="selectForm">
      <label for="musicTypes">Choose some music types, then click the button below:label>
      <select id="musicTypes" name="musicTypes" multiple>
        <option selected>R&Boption>
        <option>Jazzoption>
        <option>Bluesoption>
        <option>New Ageoption>
        <option>Classicaloption>
        <option>Operaoption>
      select>
      <button id="btn" type="button">How many are selected?button>
    form>
    
    3 kết thúc. (Nếu biểu thức
    <form name="selectForm">
      <label for="musicTypes">Choose some music types, then click the button below:label>
      <select id="musicTypes" name="musicTypes" multiple>
        <option selected>R&Boption>
        <option>Jazzoption>
        <option>Bluesoption>
        <option>New Ageoption>
        <option>Classicaloption>
        <option>Operaoption>
      select>
      <button id="btn" type="button">How many are selected?button>
    form>
    
    9 bị bỏ qua hoàn toàn, điều kiện được coi là đúng. )
  3. <form name="selectForm">
      <label for="musicTypes">Choose some music types, then click the button below:label>
      <select id="musicTypes" name="musicTypes" multiple>
        <option selected>R&Boption>
        <option>Jazzoption>
        <option>Bluesoption>
        <option>New Ageoption>
        <option>Classicaloption>
        <option>Operaoption>
      select>
      <button id="btn" type="button">How many are selected?button>
    form>
    
    23 thực thi. Để thực hiện nhiều câu lệnh, hãy sử dụng câu lệnh khối (
    <form name="selectForm">
      <label for="musicTypes">Choose some music types, then click the button below:label>
      <select id="musicTypes" name="musicTypes" multiple>
        <option selected>R&Boption>
        <option>Jazzoption>
        <option>Bluesoption>
        <option>New Ageoption>
        <option>Classicaloption>
        <option>Operaoption>
      select>
      <button id="btn" type="button">How many are selected?button>
    form>
    
    24) để nhóm các câu lệnh đó
  4. Nếu có, biểu thức cập nhật
    <form name="selectForm">
      <label for="musicTypes">Choose some music types, then click the button below:label>
      <select id="musicTypes" name="musicTypes" multiple>
        <option selected>R&Boption>
        <option>Jazzoption>
        <option>Bluesoption>
        <option>New Ageoption>
        <option>Classicaloption>
        <option>Operaoption>
      select>
      <button id="btn" type="button">How many are selected?button>
    form>
    
    25 được thực thi
  5. Điều khiển quay lại Bước 2

Thí dụ

Trong ví dụ bên dưới, hàm chứa câu lệnh

<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
3 đếm số tùy chọn đã chọn trong danh sách cuộn (phần tử
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
27 cho phép nhiều lựa chọn)

HTML

<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>

JavaScript

Ở đây, câu lệnh

<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
3 khai báo biến
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
29 và khởi tạo nó thành
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
00. Nó kiểm tra xem
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
29 có ít hơn số tùy chọn trong phần tử
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
27 hay không, thực hiện câu lệnh
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
03 tiếp theo và tăng
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
29 lên 1 sau mỗi lần đi qua vòng lặp

<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
2

làm. câu lệnh while

Câu lệnh

<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
05 lặp lại cho đến khi một điều kiện cụ thể đánh giá là sai

Một câu lệnh

<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
05 trông như sau

<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
0

<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
23 luôn được thực hiện một lần trước khi điều kiện được kiểm tra. (Để thực hiện nhiều câu lệnh, hãy sử dụng câu lệnh khối (
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
24) để nhóm các câu lệnh đó. )

Nếu

<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
09 là
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
20, câu lệnh sẽ thực hiện lại. Vào cuối mỗi lần thực hiện, điều kiện được kiểm tra. Khi điều kiện là
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
21, quá trình thực thi dừng lại và quyền điều khiển chuyển sang câu lệnh theo sau
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
05

Thí dụ

Trong ví dụ sau, vòng lặp

<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
23 lặp lại ít nhất một lần và lặp lại cho đến khi
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
29 không còn nhỏ hơn
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
25

<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
2

câu lệnh while

Một câu lệnh

<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
26 thực thi các câu lệnh của nó miễn là một điều kiện cụ thể đánh giá là
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
20. Một câu lệnh
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
26 trông như sau

<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
2

Nếu

<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
09 trở thành
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
21, thì
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
23 trong vòng lặp sẽ dừng thực thi và quyền điều khiển được chuyển đến câu lệnh sau vòng lặp

Kiểm tra điều kiện xảy ra trước khi

<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
23 trong vòng lặp được thực thi. Nếu điều kiện trả về
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
20,
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
23 được thực hiện và
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
09 được kiểm tra lại. Nếu điều kiện trả về
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
21, quá trình thực thi sẽ dừng lại và quyền điều khiển được chuyển đến câu lệnh theo sau
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
26

Để thực hiện nhiều câu lệnh, hãy sử dụng câu lệnh khối (

<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
24) để nhóm các câu lệnh đó

ví dụ 1

Vòng lặp

<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
26 sau lặp lại miễn là
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
40 nhỏ hơn
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
41

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
4

Với mỗi lần lặp lại, vòng lặp tăng

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
40 và thêm giá trị đó vào
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
43. Do đó,
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
43 và
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
40 nhận các giá trị sau

  • Sau lần vượt qua đầu tiên.
    for ([initialExpression]; [conditionExpression]; [incrementExpression])
      statement
    
    40 =
    for ([initialExpression]; [conditionExpression]; [incrementExpression])
      statement
    
    47 và
    for ([initialExpression]; [conditionExpression]; [incrementExpression])
      statement
    
    43 =
    for ([initialExpression]; [conditionExpression]; [incrementExpression])
      statement
    
    47
  • Sau khi vượt qua lần thứ hai.
    for ([initialExpression]; [conditionExpression]; [incrementExpression])
      statement
    
    40 =
    <form name="selectForm">
      <label for="musicTypes">Choose some music types, then click the button below:label>
      <select id="musicTypes" name="musicTypes" multiple>
        <option selected>R&Boption>
        <option>Jazzoption>
        <option>Bluesoption>
        <option>New Ageoption>
        <option>Classicaloption>
        <option>Operaoption>
      select>
      <button id="btn" type="button">How many are selected?button>
    form>
    
    61 và
    for ([initialExpression]; [conditionExpression]; [incrementExpression])
      statement
    
    43 =
    for ([initialExpression]; [conditionExpression]; [incrementExpression])
      statement
    
    41
  • Sau khi vượt qua lần thứ ba.
    for ([initialExpression]; [conditionExpression]; [incrementExpression])
      statement
    
    40 =
    for ([initialExpression]; [conditionExpression]; [incrementExpression])
      statement
    
    41 và
    for ([initialExpression]; [conditionExpression]; [incrementExpression])
      statement
    
    43 =
    <form name="selectForm">
      <label for="musicTypes">Choose some music types, then click the button below:label>
      <select id="musicTypes" name="musicTypes" multiple>
        <option selected>R&Boption>
        <option>Jazzoption>
        <option>Bluesoption>
        <option>New Ageoption>
        <option>Classicaloption>
        <option>Operaoption>
      select>
      <button id="btn" type="button">How many are selected?button>
    form>
    
    67

Sau khi hoàn thành bước thứ ba, điều kiện

<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
68 không còn là
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
20, do đó vòng lặp kết thúc

ví dụ 2

Tránh các vòng lặp vô hạn. Đảm bảo điều kiện trong một vòng lặp cuối cùng trở thành

<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
21—nếu không, vòng lặp sẽ không bao giờ kết thúc. Các câu lệnh trong vòng lặp
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
26 sau đây sẽ thực thi mãi mãi vì điều kiện không bao giờ trở thành
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
21

<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
6

tuyên bố có nhãn

Một

<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
93 cung cấp một câu lệnh có mã định danh cho phép bạn tham khảo nó ở nơi khác trong chương trình của mình. Ví dụ: bạn có thể sử dụng nhãn để xác định một vòng lặp, sau đó sử dụng các câu lệnh
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
94 hoặc
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
95 để cho biết chương trình nên ngắt vòng lặp hay tiếp tục thực hiện vòng lặp đó

Cú pháp của câu lệnh có nhãn giống như sau

<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
9

Giá trị của

<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
93 có thể là bất kỳ mã định danh JavaScript nào không phải là từ dành riêng.
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
23 mà bạn xác định bằng nhãn có thể là bất kỳ tuyên bố nào

Thí dụ

Trong ví dụ này, nhãn

<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
98 xác định một vòng lặp
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
26

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
0

tuyên bố phá vỡ

Sử dụng câu lệnh

<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
94 để kết thúc vòng lặp,
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
01 hoặc kết hợp với câu lệnh có nhãn

  • Khi bạn sử dụng
    <form name="selectForm">
      <label for="musicTypes">Choose some music types, then click the button below:label>
      <select id="musicTypes" name="musicTypes" multiple>
        <option selected>R&Boption>
        <option>Jazzoption>
        <option>Bluesoption>
        <option>New Ageoption>
        <option>Classicaloption>
        <option>Operaoption>
      select>
      <button id="btn" type="button">How many are selected?button>
    form>
    
    94 mà không có nhãn, nó sẽ chấm dứt ngay lập tức phần bên trong cùng của
    <form name="selectForm">
      <label for="musicTypes">Choose some music types, then click the button below:label>
      <select id="musicTypes" name="musicTypes" multiple>
        <option selected>R&Boption>
        <option>Jazzoption>
        <option>Bluesoption>
        <option>New Ageoption>
        <option>Classicaloption>
        <option>Operaoption>
      select>
      <button id="btn" type="button">How many are selected?button>
    form>
    
    26,
    for ([initialExpression]; [conditionExpression]; [incrementExpression])
      statement
    
    04,
    <form name="selectForm">
      <label for="musicTypes">Choose some music types, then click the button below:label>
      <select id="musicTypes" name="musicTypes" multiple>
        <option selected>R&Boption>
        <option>Jazzoption>
        <option>Bluesoption>
        <option>New Ageoption>
        <option>Classicaloption>
        <option>Operaoption>
      select>
      <button id="btn" type="button">How many are selected?button>
    form>
    
    3 hoặc
    for ([initialExpression]; [conditionExpression]; [incrementExpression])
      statement
    
    01 và chuyển quyền kiểm soát sang câu lệnh sau
  • Khi bạn sử dụng
    <form name="selectForm">
      <label for="musicTypes">Choose some music types, then click the button below:label>
      <select id="musicTypes" name="musicTypes" multiple>
        <option selected>R&Boption>
        <option>Jazzoption>
        <option>Bluesoption>
        <option>New Ageoption>
        <option>Classicaloption>
        <option>Operaoption>
      select>
      <button id="btn" type="button">How many are selected?button>
    form>
    
    94 với một nhãn, nó sẽ chấm dứt câu lệnh được gắn nhãn đã chỉ định

Cú pháp của câu lệnh

<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
94 trông như thế này

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
1

  1. Dạng đầu tiên của cú pháp chấm dứt vòng lặp kèm theo trong cùng hoặc
    for ([initialExpression]; [conditionExpression]; [incrementExpression])
      statement
    
    01
  2. Dạng thứ hai của cú pháp kết thúc câu lệnh có nhãn kèm theo đã chỉ định

ví dụ 1

Ví dụ sau lặp qua các phần tử trong một mảng cho đến khi tìm thấy chỉ mục của phần tử có giá trị là

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
10

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
2

ví dụ 2. Phá vỡ một nhãn

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
3

tiếp tục tuyên bố

Câu lệnh

<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
95 có thể được sử dụng để khởi động lại câu lệnh
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
26,
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
04,
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
3 hoặc
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
93

  • Khi bạn sử dụng
    <form name="selectForm">
      <label for="musicTypes">Choose some music types, then click the button below:label>
      <select id="musicTypes" name="musicTypes" multiple>
        <option selected>R&Boption>
        <option>Jazzoption>
        <option>Bluesoption>
        <option>New Ageoption>
        <option>Classicaloption>
        <option>Operaoption>
      select>
      <button id="btn" type="button">How many are selected?button>
    form>
    
    95 mà không có nhãn, nó sẽ chấm dứt lần lặp hiện tại của câu lệnh
    <form name="selectForm">
      <label for="musicTypes">Choose some music types, then click the button below:label>
      <select id="musicTypes" name="musicTypes" multiple>
        <option selected>R&Boption>
        <option>Jazzoption>
        <option>Bluesoption>
        <option>New Ageoption>
        <option>Classicaloption>
        <option>Operaoption>
      select>
      <button id="btn" type="button">How many are selected?button>
    form>
    
    26,
    for ([initialExpression]; [conditionExpression]; [incrementExpression])
      statement
    
    04 hoặc
    <form name="selectForm">
      <label for="musicTypes">Choose some music types, then click the button below:label>
      <select id="musicTypes" name="musicTypes" multiple>
        <option selected>R&Boption>
        <option>Jazzoption>
        <option>Bluesoption>
        <option>New Ageoption>
        <option>Classicaloption>
        <option>Operaoption>
      select>
      <button id="btn" type="button">How many are selected?button>
    form>
    
    3 kèm theo trong cùng và tiếp tục thực hiện vòng lặp với lần lặp tiếp theo. Ngược lại với câu lệnh
    <form name="selectForm">
      <label for="musicTypes">Choose some music types, then click the button below:label>
      <select id="musicTypes" name="musicTypes" multiple>
        <option selected>R&Boption>
        <option>Jazzoption>
        <option>Bluesoption>
        <option>New Ageoption>
        <option>Classicaloption>
        <option>Operaoption>
      select>
      <button id="btn" type="button">How many are selected?button>
    form>
    
    94,
    <form name="selectForm">
      <label for="musicTypes">Choose some music types, then click the button below:label>
      <select id="musicTypes" name="musicTypes" multiple>
        <option selected>R&Boption>
        <option>Jazzoption>
        <option>Bluesoption>
        <option>New Ageoption>
        <option>Classicaloption>
        <option>Operaoption>
      select>
      <button id="btn" type="button">How many are selected?button>
    form>
    
    95 không chấm dứt hoàn toàn việc thực hiện vòng lặp. Trong vòng lặp
    <form name="selectForm">
      <label for="musicTypes">Choose some music types, then click the button below:label>
      <select id="musicTypes" name="musicTypes" multiple>
        <option selected>R&Boption>
        <option>Jazzoption>
        <option>Bluesoption>
        <option>New Ageoption>
        <option>Classicaloption>
        <option>Operaoption>
      select>
      <button id="btn" type="button">How many are selected?button>
    form>
    
    26, nó nhảy trở lại điều kiện. Trong một vòng lặp
    <form name="selectForm">
      <label for="musicTypes">Choose some music types, then click the button below:label>
      <select id="musicTypes" name="musicTypes" multiple>
        <option selected>R&Boption>
        <option>Jazzoption>
        <option>Bluesoption>
        <option>New Ageoption>
        <option>Classicaloption>
        <option>Operaoption>
      select>
      <button id="btn" type="button">How many are selected?button>
    form>
    
    3, nó nhảy tới
    for ([initialExpression]; [conditionExpression]; [incrementExpression])
      statement
    
    24
  • Khi bạn sử dụng
    <form name="selectForm">
      <label for="musicTypes">Choose some music types, then click the button below:label>
      <select id="musicTypes" name="musicTypes" multiple>
        <option selected>R&Boption>
        <option>Jazzoption>
        <option>Bluesoption>
        <option>New Ageoption>
        <option>Classicaloption>
        <option>Operaoption>
      select>
      <button id="btn" type="button">How many are selected?button>
    form>
    
    95 với nhãn, nó sẽ áp dụng cho câu lệnh lặp được xác định bằng nhãn đó

Cú pháp của câu lệnh

<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
95 như sau

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
4

ví dụ 1

Ví dụ sau đây cho thấy một vòng lặp

<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
26 với câu lệnh
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
95 thực thi khi giá trị của
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
29 là
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
41. Do đó,
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
40 nhận các giá trị
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
47,
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
41,
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
34 và
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
35

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
5

Nếu bạn nhận xét

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
36, vòng lặp sẽ chạy cho đến hết và bạn sẽ thấy
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
37

ví dụ 2

Câu lệnh có nhãn

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
38 chứa câu lệnh có nhãn
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
39. Nếu gặp phải
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
95, chương trình sẽ kết thúc lần lặp hiện tại của
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
39 và bắt đầu lần lặp tiếp theo. Mỗi lần gặp phải
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
95,
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
39 lặp lại cho đến khi điều kiện của nó trở lại
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
21. Khi
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
21 được trả về, phần còn lại của câu lệnh
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
38 được hoàn thành và
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
38 lặp lại cho đến khi điều kiện của nó trả về
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
21. Khi trả về
<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
21, chương trình tiếp tục tại câu lệnh sau
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
38

Nếu

<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
95 có nhãn là
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
38, chương trình sẽ tiếp tục ở đầu câu lệnh
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
38

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
6

vì. trong tuyên bố

Câu lệnh

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
54 lặp lại một biến được chỉ định trên tất cả các thuộc tính có thể đếm được của một đối tượng. Đối với mỗi thuộc tính riêng biệt, JavaScript thực thi các câu lệnh đã chỉ định. Một câu lệnh
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
54 trông như sau

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
7

Thí dụ

Hàm sau đây nhận đối số của nó là một đối tượng và tên của đối tượng. Sau đó, nó lặp lại trên tất cả các thuộc tính của đối tượng và trả về một chuỗi liệt kê tên thuộc tính và giá trị của chúng

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
8

Đối với một đối tượng

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
56 với các thuộc tính
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
57 và
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
58, thì
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
59 sẽ là

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
9

Mảng

Mặc dù có thể muốn sử dụng điều này như một cách để lặp lại các phần tử

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
60, nhưng câu lệnh
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
54 sẽ trả về tên của các thuộc tính do người dùng xác định bên cạnh các chỉ mục số

Do đó, tốt hơn là sử dụng vòng lặp

<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
3 truyền thống với chỉ mục số khi lặp qua các mảng, bởi vì câu lệnh
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
54 lặp qua các thuộc tính do người dùng xác định ngoài các phần tử mảng, nếu bạn sửa đổi đối tượng Array (chẳng hạn như thêm các thuộc tính tùy chỉnh hoặc

vì. tuyên bố

Câu lệnh

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
64 tạo một vòng lặp Lặp lại trên các đối tượng có thể lặp lại (bao gồm đối tượng
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
60,
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
66,
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
67,
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
68, v.v.), gọi một hook lặp tùy chỉnh với các câu lệnh sẽ được thực thi cho giá trị của từng thuộc tính riêng biệt

<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
0

Ví dụ sau đây cho thấy sự khác biệt giữa vòng lặp

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
64 và vòng lặp
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
54. Trong khi
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
54 lặp lại tên thuộc tính, thì
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
64 lặp lại giá trị thuộc tính

<form name="selectForm">
  <label for="musicTypes">Choose some music types, then click the button below:label>
  <select id="musicTypes" name="musicTypes" multiple>
    <option selected>R&Boption>
    <option>Jazzoption>
    <option>Bluesoption>
    <option>New Ageoption>
    <option>Classicaloption>
    <option>Operaoption>
  select>
  <button id="btn" type="button">How many are selected?button>
form>
1

Các câu lệnh

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
64 và
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
54 cũng có thể được sử dụng với hàm hủy. Ví dụ: bạn có thể lặp đồng thời các khóa và giá trị của một đối tượng bằng cách sử dụng
for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement
75