Javascript Arrays

Javascript Arrays

What are Arrays in Javascript?

A javascript Array is a single variable which is used to store elements of different data types.

Arrays start with index 0.

Different ways to Declare Arrays

1st Way

// Initializing while declaring
let numberList = [1,2,3,4,5];

2nd Way

// Initializing while declaring using new keyword
let numberList = new Array(1,2,3);

Accessing Elements of an Array

Any element in the array can be accessed using the index number. The index in the arrays starts with 0.

const numbers = ["One","Two","Three"];
console.log(numbers[0]) // prints One
console.log(numbers[1]) // prints Two
console.log(numbers[2]) // prints Three

Array Methods

length

array.length is used to get the size or length of array.

const numbers = ["One","Two","Three"];
console.log(numbers.length); // prints 3

// Get last element of array
console.log(numbers[numbers.length-1]); // Three

Push

push() inserts a new value at the end of the array.

const numbers = ["One","Two","Three"];
numbers.push("Four"); // Inserts Four after Three
console.log(numbers);

Slice

slice() method is used to get values from a particular range.

const numbers = ["One","Two","Three","Four","Five"];
console.log(numbers.slice(1,3)); // Prints Two & Three

Splice

splice() is used to insert from a particular index and remove elements from the same index in a single method.

const numbers = ["One","Two","Three","Four","Five"];

//inserts Six,Seven from 2nd index and removes 1 value from index 2
numbers.splice(2,1,"Six",Seven);

console.log(numbers.slice(1,3));
//Output - [ 'One', 'Two', 'Six', 'Seven', 'Four', 'Five' ]

Concat

concat() is used to concatenate all the elements of another into an array.

const numbers = ["One","Two","Three","Four","Five"];
const numbers2 = ["Six","Seven"];
// Following line concatenates all elements of numbers2 array into numbers array.
console.log(numbers.concat(numbers2));

Fill

fill() is used to fill or insert a particular value into a particular index range.

let arr = [1,2,3,4,5,6,7,8,9];
// following snuppet fills value 'Jay' to elements from range 2-4
arr.fill('Jay',2,4);
console.log(arr); // Output - [1,2,'Jay','Jay',5,6,7,8,9];

Includes

includes() checks for a value is present at a particular index and returns true if the value is found.

let num = [1,2,3,4,5,6,7,8];
// checks for value 7 is present at index 6
console.log(num.includes(7,6)); // Output - true
// checks for value 3 is present at index 6
console.log(num.includes(3,6)); // Output - false

indexOf & lastIndexOf

indexOf() finds and returns the first index of the value if it is repeating in array.

lastIndexOf() finds and returns the last index of the value if it is repeating in array.

let num = [1,2,3,'Jay',5,6,'Jay','Jay'];
// returns first index of value 'Jay'
console.log(num.indexOf('Jay')); // Output - 3
// returns last index of value 'Jay'
console.log(num.lastIndexOf('Jay')); // Output - 7

isArray

Array.isArray() checks for the variable is an array or not and returns true if it is an array.

const number = "One";
const numbers = ["One","Two","Three","Four","Five"];
console.log(Array.isArray(number)); // Output - false
console.log(Array.isArray(numbers)); // Output - true

join

join() creates and returns a string by seperating by a , or specified string.

const numbers = ['One','Two','Three'];
console.log(elements.join()); // Output - "One,Two,Three"
console.log(elements.join('-')); // Output - "One-Two-Three"

split

split() is used to convert the given string into an array.

const numberString = "one,two,three,four";
const numbers = numberString.split(',');
console.log(numbers); // Output - [ 'one', 'two', 'three', 'four' ]

map

map() method creates a new array containing the output of the calling function on every element in the calling array.

let maths = [1,4,9,16,25];
console.log(maths.map(Math.sqrt)); // Output - [ 1, 2, 3, 4, 5 ]

filter

filter() method is used to filter down the elements of the array based on certain conditions which are passed as the function to the method. If the element satisfies the condition then the element is added to the new array else it won't be added to the array.

const numbers = [1,2,3,4,5,6,7,8,9,10]
const evenNumbers = numbers.filter( a => a%2==0)
console.log(evenNumbers) // Output - [ 2, 4, 6, 8, 10 ]

pop

pop() method remove/pops out an element from the end of an array.

const numbers = ['One','Two','Three'];
console.log(numbers.pop()); // Output - ['One','Two']

shift

shift() removes one element or shifts the element from the start to the left.

const numbers = ['One','Two','Three'];
console.log(numbers.shift()); // Output - ['Two','Three'];

unshift

unshift() shifts elements to right or inserts elements from the start and returns the index after shifting.

let numbers = ['One','Two','Three'];
console.log(numbers.unshift('Four','Five')); // Output - 5
console.log(numbers); // [ 'Four', 'Five', 'One', 'Two', 'Three']

sort & reverse

sort() is used to sort the array in ascending order.

reverse() is used to reverse all the elements array.

let names = ["Hitesh Sir","Anurag Sir","Jay","Sumit","Ninad"];

// sorts the array in ascending order
console.log(names.sort()); 
// Output - [ 'Anurag Sir', 'Hitesh Sir', 'Jay', 'Ninad', 'Sumit' ]

// sorts the array in descending order
console.log(names.reverse(names.sort()));
// Output - [ 'Sumit', 'Ninad', 'Jay', 'Hitesh Sir', 'Anurag Sir' ]

Thanks for Reading...

Did you find this article valuable?

Support Jay Malde by becoming a sponsor. Any amount is appreciated!