If you have any query feel free to chat us!
Happy Coding! Happy Learning!
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.
Comments: 2
I am not able to access videos from second class and further. I have already completed first class
When will I get my course?
Now, Your query was resolved.