Loops are handy, if you want to run the same code over and over again, each time with a different value. Show Often this is the case when working with arrays: Instead of writing:text += cars[0] + " You can write:for (let i = 0; i < cars.length; i++) { Different Kinds of LoopsJavaScript supports different kinds of loops:
The For LoopThe for (expression 1; expression 2; expression 3) { Expression 1 is executed (one time) before the execution of the code block. Expression 2 defines the condition for executing the code block. Expression 3 is executed (every time) after the code block has been executed. Examplefor (let i = 0; i < 5; i++) { From the example above, you can read: Expression 1 sets a variable before the loop starts (let i = 0). Expression 2 defines the condition for the loop to run (i must be less than 5). Expression 3 increases a value (i++) each time the code block in the loop has been executed. Expression 1Normally you will use expression 1 to initialize the variable used in the loop (let i = 0). This is not always the case. JavaScript doesn't care. Expression 1 is optional. You can initiate many values in expression 1 (separated by comma): Examplefor (let i = 0, len = cars.length, text = ""; i < len; i++) { And you can omit expression 1 (like when your values are set before the loop starts): Examplelet i = 2; Expression 2Often expression 2 is used to evaluate the condition of the initial variable. This is not always the case. JavaScript doesn't care. Expression 2 is also optional. If expression 2 returns true, the loop will start over again. If it returns false, the loop will end. If you omit expression 2, you must provide a break inside the loop. Otherwise the loop will never end. This will crash your browser. Read about breaks in a later chapter of this tutorial. Expression 3Often expression 3 increments the value of the initial variable. This is not always the case. JavaScript doesn't care. Expression 3 is optional. Expression 3 can do anything like negative increment (i--), positive increment (i = i + 15), or anything else. Expression 3 can also be omitted (like when you increment your values inside the loop): Examplelet i = 0; Loop ScopeUsing Examplevar i = 5; for (var i = 0; i < 10; i++) { // Here i is 10 Try it Yourself »Using Examplelet i = 5; for (let i = 0; i < 10; i++) { // Here i is 5 Try it Yourself »In the first example, using In the second example, using When Untuk dapat menampilkan data dalam suatu array, kita dapat menyebut nama array dengan indeks array. Namun jika kita ingin menampilkan semua isi dalam array tentunya cara tersebut tidaklah efektif. Maka cara yang paling tepat adalah dengan menggunakan perulangan atau looping. Berikut contoh script untuk menampilkan isi array dengan menyebutkan indeks array:
Contoh diatas akan menampilkan isi array dengan indeks 1 yaitu Senin. Untuk dapat menampilkan seluruh isi array, kita dapat menggunakan beberapa cara, diantaranya yaitu: Menggunakan document.write
Dengan menggunakan document.write maka seluruh isi array akan ditampilkan dengan tanda koma sebagai pemisahnya. Contoh
Script diatas akan menghasilkan tampilan yang sama dengan proses pengulangan dengan menggunakan for diatas. Namun pada script dengan menggunakan for ... in kita tidak ditutut untuk mengetahui jumlah elemen array dan jenis indeks array. |