Iterating over arrays

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Iterating over arrays

To iterate over an array in JavaScript, there are several approaches.

One common approach is to use a for loop with the array's length property. Here's an example:

cssCopy code

let array = [1, 2, 3, 4, 5]; for (let i = 0; i < array.length; i++) {  console.log(array[i]); }

This loop starts at index 0 and continues until i is no longer less than the length of the array. The loop body logs each element of the array to the console.

Another approach is to use the forEach method, which is available on all arrays. The forEach method takes a callback function as an argument and calls that function once for each element in the array. Here's an example:

javascriptCopy code

let array = [1, 2, 3, 4, 5]; array.forEach(function(element) {  console.log(element); });

This code does the same thing as the for loop example, but using the forEach method instead.

A third approach is to use the for...of loop introduced in ECMAScript 2015. This loop allows you to iterate over the values of an iterable object (like an array) without needing to access the index explicitly. Here's an example:

cCopy code

let array = [1, 2, 3, 4, 5]; for (let element of array) {  console.log(element); }

This loop logs each element of the array to the console, just like the previous examples.

16. JavaScript - Functions and Array

2 Comments

@niteshguptav63
niteshguptav63 Nov 17, 2024 at 1:39 PM

I am not able to access videos from second class and further. I have already completed first class

@niteshguptav63
niteshguptav63 Nov 16, 2024 at 10:56 AM

When will I get my course?

@admin79
admin79 Nov 17, 2024 at 1:29 PM

Now, Your query was resolved.

Frequently Asked Questions About Sciaku Courses & Services

Quick answers to common questions about our courses, quizzes, and learning platform

Didn't find what you're looking for?

help_center Contact Support