Arrays are one of JavaScript's most important data structures. Most data is stored in an array format.
In this article I will be covering arrays and the various methods of accessing arrays:
Accessing the values in an array
Changing the values in an array
Adding to an array
What methods are used in an array?
Conclusion
What is an array?
Arrays are part of the JavaScript data structure; they are collections of data, such as comments, photos, posts, songs, poems, numbers, colors, and so on.
It is denoted by square or angular brackets [].
syntax of an array
const name= []
An array is stored in a variable, followed by the name and assigned; each value is separated by a comma.
const colors = [ 'purple', 'blue' , 'orange' ]
console.log (colors) //output It tells you that it is an array with three values.
Accessing the values in an array
In JavaScript, each value is assigned an index number, so when accessing, you must specify the index number assigned to that item in the array.
JavaScript is zero-based it starts counting from 0.
The examples below show an array that has not yet been assessed and an array that has been assessed.
const colors = [ 'purple', 'blue', 'orange' ]
console.log (colors) //output 0-purple, 1-blue, 2-orange.
An array that is assessed
const colors = ['purple', 'blue', 'orange']
console.log(colors [1]) // output will be blue.
Changing the value in an array
To change a value in an array:
The array has to be defined first
Assigned the right index number of the item in the array you want to change.
example
const colors = ['purple', 'blue', 'orange']
colors[1] = 'pink'
console.log (colors) // output [purple, pink, orange]
In the above example, I changed the value of the item blue to pink by using the index and that index (1).
purple - index 0, blue - index 1, orange - index 2
Adding to an array
To add to an array simply use the length property that will add the item and return a new array.
example
const colors = ['purple', 'blue', 'orange'];
colors[colors.length] = 'black';
console.log(colors) // output ['purple', 'blue', 'orange', 'black']
What are the methods in an array?
There are several methods used in an array but I will be discussing some common methods:
shift()
unshift()
pop()
push()
concat()
join()
The first method I'd like to discuss is:
Shift method
The shift()method deletes one value from the beginning of an array.
example
let name = ['josh', 'white', 'harrison', 'david'];
name.shift();
console.log(name); // output ['white', 'harrison', 'david']
Unshift method
The unshift () method adds to the start of an array
let's add joshback
example
let name = ['white', 'harrison', 'david'];
name.unshift('josh');
console.log(name); // output [ 'josh', 'white', 'harrison', 'david']
Pop() method
This method removes an item from the array's end.
example
let name = ['josh', 'white', 'harrison', 'david'];
name.pop();
console.log(name); // output ['josh' 'white', 'harrison']
Push method
The push() method is the opposite of the pop()method in that it adds a value or item to the end of the array.
example
let name = ['josh', 'white', 'harrison', 'david'];
name.push('joy');
console.log(name); // output ['josh' 'white', 'harrison', 'david', 'joy']
Join method
The join() method creates and returns a new string by concatenating all the comma-separated items in an array.
For example
let items = ['bag', 'shoe', 'book', 'playstation'];
let result = items.join("/");
console.log(result); // output is bag/shoe/book/playstation
Concat() method
This method concatenates and creates a new array by joining the previous array, resulting in a new array.
example
const boyName = ['josh', 'white', 'john'];
const girlName = ['crystal', 'rose', 'monica'];
let result = boyName.concat(girlName);
console.log(result); // output ['josh', 'white', 'john', 'crystal', 'rose', 'monica']
Conclusion
Keynotes:
Always remember that to assess or modified an item in array that JavaScript starts from zero to counts.
It is critical to understand when to use a specific method in an array.
To create an array, always store it in a variable.
Note: The methods listed here are the most commonly used; we have additional methods in an array.