Loops offer a quick and easy way to do something repeatedly. This chapter of the
JavaScript Guide introduces the different iteration statements available to JavaScript. You can think of a loop as a computerized version of the game where you tell someone to take X steps in one direction, then Y steps in another. For example, the idea "Go five steps to the east" could be expressed this way as a loop: There are many different kinds of loops, but they all essentially do the same thing: they repeat an action some number of times. (Note that it's possible that number could be zero!) The various loop mechanisms offer different ways to determine the start and end points of the loop. There are various situations that are more easily served by one type of loop over the others. The statements for loops provided in JavaScript are:
for statementA A
When a
Example In the example below, the function contains a HTML
JavaScriptHere, the
do...while statement The A
If Example In the following example, the
while statement A
If the The condition test occurs before To execute multiple statements, use a block statement ( Example 1 The following
With each iteration, the loop increments
After completing the third pass, the condition Example 2 Avoid infinite loops. Make sure the condition in a loop eventually becomes
labeled statement A The syntax of the labeled statement looks like the following: The value of ExampleIn this example, the label
break statement Use the
The syntax of the
Example 1 The following example iterates through the elements in an array until it finds the index of an element whose value is
Example 2: Breaking to a label
continue statement The
The syntax of the
Example 1 The following example shows a
If you comment out the Example 2 A statement labeled If
for...in statement The
ExampleThe following function takes as its argument an object and the object's name. It then iterates over all the object's properties and returns a string that lists the property names and their values.
For an object car.make = Ford car.model = Mustang Arrays Although it may be tempting to
use this as a way to iterate over Therefore, it is better to use a traditional for...of statement The
The following example shows the difference between a
Bagaimana cara memanggil fungsi di Javascript?Cara Memanggil/Eksekusi Fungsi
Kita bisa memanggil fungsi di dalam kode Javascript dengan menuliskan nama fungsinya seperti ini: namaFungsi(); Contoh: // membuat fungsi function sayHello(){ console.log("Hello World!"); } // memanggil fungsi sayHello() // maka akan menghasilkan -> Hello World!
Apa itu iterator pada Javascript?Iterator adalah properti dari suatu kumpulan JavaScript. Iterator meningkatkan efisiensi dengan memungkinkan Anda mengonsumsi item yang ada dalam daftar satu per satu seolah-olah itu adalah aliran.
Apa cara yang benar untuk menulis array Javascript?Cara Membuat Array pada Javascript
Pada javascript, array dapat kita buat dengan tanda kurung siku ( [...] ). Contoh: var products = []; Maka variabel products akan berisi sebuah array kosong.
Foreach di Javascript untuk apa?Perulangan Foreach di Javascript
Perulangan foreach biasanya digunakan untuk mencetak item di dalam array. Perulangan ini termasuk dalam perulangan counted loop, karena jumlah perulangannya akan dituentukan oleh panjang dari array.
|