Arrays in Javascript

Arrays in Javascript

Array's properties and methods

What is an Array ?

An array allows you to store several values with a single variable name and access them by using an index number. It is a collection of data separated by commas (","). Data can be of the same or distinct data types.

Syntax

var arrayName = [1, 2, 3, "apple", "name", true]

Accessing Array Element

Array elements in JavaScript are indexed from 0, therefore we may access them as follows:

var arrayName = [1, 2, 3, "apple", "name", true]

console.log(arrayName[0])
console.log(arrayName[4])
console.log(arrayName[5])

Output:

1
name
true

Properties of an Array

length

The length property returns the number of elements in an array.

var array1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
console.log(arrayName.length)

var array2 = ["a", "b", "c", "d", "e" ]
array2.length = 3
console.log(array2)

Output:

9
['a', 'b', 'c']

Methods of an Array

concat()

It joins arrays and returns an array containing the merged arrays. The returned new array contains the values of the connected arrays but makes no changes to the original arrays.

var arr1 = [2, 4, 6, 8]
var arr2 = [10, 12, 14, 16]
var arr = arr1.concat(arr2)
console.log.(arr)

Output:

[
2, 4, 6, 8,
10, 12, 14, 16
]

push()

A new item is added to the end of an array using the push() method.

var names = ["james", "thomas", "julie", "nancy"]
names.push("tom")
console.log(names)

Output:

[ 'james', 'thomas', 'julie', 'nancy', 'tom' ]

pop()

It removes the last element from an array and returns that element.

var nums = [1, 2, 3, 4, 5, "apple"]
nums.pop()
console.log(nums)

Output:

[1, 2, 3, 4, 5]

shift()

The first item in an array is deleted using the shift() method. The initial array is altered via the shift() method.

var arr = [1, "a", "b", "c"]
arr.shift()
console.log(arr)

Output:

[ 'a', 'b', 'c' ]

unshift()

A new element is added to the beginning of an array with the unshift() method. The initial array is replaced with the unshift() method.

var nums = [1, 2, 3, 4, 5, 6]
nums.unshift(-1, 0)
console.log(nums)

Output:

[
  -1, 0, 1, 2,
   3, 4, 5, 6
]

indexOf()

The indexOf() method locates the requested item in the array and returns its first position. -1 is returned by the indexOf() method if the value cannot be found.

var fruits = ["apple", "banana", "pear", "watermelon", "papaya", "watermelon"]
var ind = fruits.indexOf("watermelon")
console.log(ind)

Output:

3

lastIndexOf()

The indexOf() method locates the requested item in the array and returns its last position. -1 is returned by the indexOf() method if the value cannot be found.

var fruits = ["apple", "banana", "pear", "watermelon", "papaya", "watermelon"]
var ind = fruits.lastIndexOf("watermelon")
console.log(ind)

Output:

5

isArray()

If an object is an array, the isArray() function returns true; otherwise, it returns false.

var nums = [1, 2, 3, 4]
var isArr = Array.isArray(nums)
console.log(isArr)

Output:

true

slice()

Slice() creates a new array from the array's chosen elements. The slice() method makes choices between a specified start and a non-inclusive finish. The slice() method does not change the original array.

var arr = ["pink", "yellow", "red", "black", "blue"]
var sliceArr = arr.slice(0, 3)
console.log(sliceArr)

Output:

[ 'pink', 'yellow', 'red' ]

every()

The every() method determines whether all array elements pass the test implemented by the provided function. It returns a boolean value.

var ages = [12, 18, 24, 35, 62, 70, 84]

var result = ages.every(checkSeniorCitizen)

console.log(result)

function checkSeniorCitizen(age) {
            return age > 60
}

Output:

false

reverse()

The order of the elements in an array is reversed via the reverse() method. The reverse() method overwrites the original array.

var arr = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
var result = arr.reverse()
console.log(result)

Output:

[1, 2, 3, 4,  5, 6, 7, 8, 9, 10]

sort()

An array's elements are sorted via the sort() method. The original array is overwritten by sort(). The sort() method uses ascending and alphabetical order to sort the data as strings.

var arr = [2, 5, 1, 3, 4, "apple", 5, 6]
var result = arr.sort()
console.log(result)

Output:

[ 1, 2, 3, 4, 5, 5, 6, 'apple' ]

filter()

A new array is created by the filter() method and contains just the entries that pass the test given by the function. The function for empty items is not run by the filter() method.

var ages = [12, 18, 24, 35, 62, 70, 84]

var result = ages.filter(checkSeniorCitizen)

console.log(result)

function checkSeniorCitizen(age) {
            return age > 60
}

Output:

[ 62, 70, 84 ]

forEach()

For every element of an array, the forEach() method calls a given function once.

var names = ["james", "thomas", "julie", "nancy"]
names.forEach(function(data){
        console.log(data.toUpperCase())
})

Output:

JAMES
THOMAS
JULIE
NANCY

These are a handful of the most useful Array properties and methods. I hope you found this article informative.

🧑‍💻THANK YOU SO MUCH! Have fun coding! 🧑‍💻