Last updated Sep 22, 2022

Array traversing and accessing in Data structure with JavaScript

In this article, we will learn about Array traversing and accessing Data structure using JavaScript.

Array traversing and accessing in Data structure with JavaScript

In this article, we will learn about Array traversing and accessing Data structure using JavaScript.

Learn Array traversing and accessing in Data structure

What is Array Traversing?

Array Travesing means to access each element (item) stored in the array so that the data can be checked or used as part of a process.

The array basically contains multiple elements of the same type, We can traverse an array directly by element index, like below:

let arr = [3, 5, 85, 0, 13, 51];

document.write(`Array ${0} = ${arr[0]} <br>`)
document.write(`Array ${1} = ${arr[1]} <br>`)
document.write(`Array ${2} = ${arr[2]} <br>`)
document.write(`Array ${3} = ${arr[3]} <br>`)
document.write(`Array ${4} = ${arr[4]} <br>`)
document.write(`Array ${5} = ${arr[5]} <br>`)

Output:

Array 0 = 3
Array 1 = 5
Array 2 = 85
Array 3 = 0
Array 4 = 13
Array 5 = 51

The above method does not work efficiently when the array is bigger and its also not good pratice to do this, so in this case we can use for loop to raver an array like this:

let arr = [3, 5, 85, 0, 13, 51];
for(let i=0;i<arr.length;i++)
{
     document.write(`Array ${i} = ${arr[i]} <br>`)
}

Output:

Array 0 = 3
Array 1 = 5
Array 2 = 85
Array 3 = 0
Array 4 = 13
Array 5 = 51

Tags

Available for projectsLet’s build together

Need this built properly,
not just explained?

I take on Angular and NestJS work directly — auth systems, multi-tenant SaaS, API rewrites and the deploy pipeline underneath them. You talk to the person writing the code.

Projects delivered
20+ Projects delivered
Angular • NestJS
7+ years Angular • NestJS
Average reply time
1 day Average reply time

What I can help with

  • Auth and permissions systems
  • Multi-tenant SaaS foundations
  • API design and rewrites
  • Deploy pipelines and VPS setup
  • Bug fixing and performance optimization
Send me a message

Tell me about your project and I’ll get back with a realistic scope and price.

Turn
Your Ideas
Into Reality

Stay in the loop

Get the finished tutorials, projects and resources in your inbox.

Join 5,000+ developers and get new tutorials, source code, product updates and exclusive content — straight to your inbox.

  • No spam
  • Useful content only
  • Unsubscribe anytime

Subscribe to TutsCoder

Be the first to get new tutorials, projects and tips.

Join 5,000+ developers already learning with us.

Learn
Build
Grow

Discussion

Comments

Share your thoughts, ask questions or suggest improvements. Let’s learn and grow together!

Loading comments…

Join the discussion

Sign in to join the discussion. Your account also keeps your saved tutorials and path progress.