loop array javascript

How to Loop Array in Javascript in Right Way

My focus to the programming language PHP, but lately I am busy creating applications using the JavaScript programming language. And the environment that I choose is the node JS. As I did yesterday is to create applications that can capture the URL of a Google image search. Then I developed this app again to be able to directly download images from Google image search without going through a third-party program such as Download Manager. And sure enough, the application can run well and it saves time more than 60%.

Fortunately, in this world there is node JS. I was greatly helped by this framework, because we can make for very easy especially with many developers who create reusable code that can be accessed for free.

How to Access an Array in Javascript in Right Way

loop array javascript

In this article I will discuss how to access the array in javascript using looping. If in php to use the loop we can use the for syntax or while

But on javascript when we use for to access the array, then the result is not always valid. I do not know why the array being accessed is sometimes undetectable, but programmatically it is correct.

Examples like this

for (i = 0; i <= url.length; i ++) {
 console.log (url [i]);
}

In some cases it could work well, meaning we can access the array data using looping for. But in other cases, for example as below

for (i = 0; i <= url.length; i ++) {
 console.log (url [i]);
 console.log ( “Again” + url [i]);
}

In the second line, we could not access the array. I do not know why it happened

The solution is, it turns out the array object has a special function for the loop, so we do not need to do looping in for.

Examples like this

url = [ “https://google.com”, “https://freshwebdev.com”];
url.forEach (function (item, index, array) {
  console.log (item, index);
});
// Https://google.com 0
// Https://freshwebdev.com 1

By using the special function, we can access the entire array perfectly.

In conclusion, JavaScript is a programming language that is good and complete. So naturally javascript Is a modern programming language that is constantly evolving.

No Responses

Show all responses

Write a response