This post is originally posted to my blog.
In the post I will share some basics about Array.
Array
Array is the way of storing group or list of data.
Let say, we want to store group of colors without using array.
const color1 = 'purple';
const color2 = 'black';
const color3 = 'yellow';
const color4 = 'aqua';
As we see, our codes are not DRY(Don't Repeat Yourself) at all. They are WET(Write Everything Twice), this is not a good practice for writing good code. We can use Array to solve this problem.
const colors = ['purple', 'black', 'yellow', 'aqua'];
How to create Array
You can start with empty Array and then add data later, or you can start with it's data;
// empty array
const colors = [];
// with data
const colors = ['purple', 'black', 'yellow', 'aqua'];
Add data into array
There two way I known for adding data into the Array, 'bracket notation' and array methods.
i. By bracket notation
Array are indexed starting from 0.
const colors = [];
// Add first data
colors[0] = 'purple';
// So the second item
colors[1] = 'black';
console.log(colors); // ['purple', 'black'];
ii. By Array methods
If you want to add item at very first position of Array use unshift method, at very end use push method.
const colors = ['purple', 'black'];
// use unshift method to add to front
colors.unshift('aqua');
console.log(colors); // ['aqua', 'purple', 'black'];
// use push method to add to end
colors.push('yellow');
console.log(colors); // ['aqua', 'purple', 'black', 'yellow'];
Access data from Array
You can access data from Array by using bracket notation.
const colors = ['purple', 'black', 'yellow', 'aqua'];
// black and aqua
colors[1]; // 'black'
colors[3]; // 'aqua'
Also you can access array's item by loop over it.
const colors = ['purple', 'black', 'yellow', 'aqua'];
for(const color of colors) {
console.log(color); // purple black yellow aqua
}
Update data of Array
Also you can use bracket notation to update array's data.
const colors = ['purple', 'black'];
// update black to yellow
colors[1] = 'yellow';
console.log(colors); // ['purple', 'yellow'];
Array can hold any data type and can be nested.
const data = [24, true, ['orange', null], undefined];
Top comments (10)
I think you should be using for(let color of colors) not for(const color of colors). What do you think !?
In most case let and const are almost same, the only difference I known, const cannot be updated but let can.
Let use this example for demonstration
Then their aren't same anymore...
Yes i agree with you on that, but i think that’s a miss use of const because inside the loop the variable color is changing at each iteration.
When use const in for of loop, in each iteration you get a new variable, which is scoped only to that iteration.
I use let only when i want to modify variable's value.
In below example you can't use const because the value of variable is modified by increment one until the condition is false.
In below example, the constant variable(random) hold each values of that array.
Remember, to be constant does not mean to hold only single data. const can also hold list of data. Good example is array.
How to delete some item from array?
You can delete by using splice method
Hey, really nice and useful article, great job !!
Thanks friend