DEV Community

Cover image for Enough JavaScript to get you Started : #9 Array
Adarsh Pandya
Adarsh Pandya

Posted on

Enough JavaScript to get you Started : #9 Array

The definition resides in usage of Array

πŸ‘‰ imagine someone gave you a definition which says store all the fruits and display them

πŸ‘‰ Idea 1 (foolish way to manage things) : try to create 30 to 40 variables

πŸ‘‰ Idea 2 (smarter way to manage things) : creating a Array named fruits

What is Array anyway?

πŸ‘‰ Arrays are just collection of data types

πŸ‘‰ I'm sure if you are beginner the line i written above will not make any sense to you

πŸ‘‰ Imagine creating a one big variable or container which can hold multiple small variables ,so it's easy to access

πŸ‘‰ Real life example : box of chocolates 🍫🍫🍫🍫🍫 which can hold 100s of chocolates , now imagine managing 100 chocolates without box (pretty messed up right ? 🀣)

Graphical Representation of Array

Artboard – 7.png

πŸ‘‰ Array holds multiple values, whereas an ordinary variable hold a single value

πŸ‘‰ Array values must be inside Square braces [ ]

πŸ‘‰ Arrays are index based and index starts from 0

πŸ‘‰ In simple words if you want to access "Apple", you need to write

fruits = ["apple","banana","grapes,"cherry"]

console.log(fruits[1]); ❌

console.log(fruits[0]); βœ…
// logs "apple"

Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ Printing Whole Array

fruits = ["apple","banana","grapes,"cherry"];

console.logs(fruits);

//logs ["apple","banana","grapes,"cherry"]
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ Iterating over individual elements of array

fruits = ["apple","banana","grapes,"cherry"];

// for of generally used for arrays

for (var fruit of fruits)
{
      console.log(fruit);
}
Enter fullscreen mode Exit fullscreen mode

Let me know in comment section if you have any doubt or feedback. it's always worth to give time to thriving developer community :)

Keep Coding ❀

Hey , Let' ConnectπŸ‘‹

Twitter / Github

Top comments (0)