𝗔𝗿𝗿𝗮𝘆𝘀 𝗜𝗻 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁
An array stores multiple values in one variable. Instead of creating separate variables for every item, you group them together.
How to create an array Use square brackets to list your items. const bikes = ["pulser", "apache", "duke"];
Key facts about arrays:
- Arrays are objects. The typeof operator returns "object" for them.
- They use numbers to access data. The first item is at index 0.
- You can store different types of data in one array. You can store strings, numbers, objects, or even other arrays.
Common tasks:
- Convert to string: Use the toString() method to turn your list into a comma separated string.
- Add items: Use the push() method to add a new item to the end of your list.
- Loop through items: Use a for loop to visit every element in your array.
Arrays vs Objects You must choose the right tool for your data.
- Use arrays when your index is a number.
- Use objects when your index is a string (text).
JavaScript does not support associative arrays. If you need named indexes, use an object instead.
Complex data structures You can nest data to build complex models. An object can contain an array. An array can contain objects. This helps you manage real world data structures.
Source: https://www.w3schools.com/js/js_arrays.asp Source: https://www.geeksforgeeks.org/javascript/best-known-javascript-array-methods/
Full post: https://dev.to/ezhil_arasan_d1230a486501/array-in-javascrip-1hna