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

Comments: 2

profile
@niteshguptav63
17-Nov-2024, 01:39 PM

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

profile
@niteshguptav63
16-Nov-2024, 10:56 AM

When will I get my course?

profile
@admin79
17-Nov-2024, 01:29 PM

Now, Your query was resolved.

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?