Enough JavaScript to get you Started : #9 Array
Adarsh Pandya
Posted on January 15, 2021
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
π 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"
π Printing Whole Array
fruits = ["apple","banana","grapes,"cherry"];
console.logs(fruits);
//logs ["apple","banana","grapes,"cherry"]
π 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);
}
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π
Posted on January 15, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
January 27, 2022