Arrays

What is Arrays?

Arrays is a variable which stores multiple values. They are list like objects.


Creating Arrays

Different ways of creating array

var a = []; // creates an empty array
// following creates an array with two elements i.e 'y' and 'w'
var a = ['w', 'y'];

// create an array with 3 empty elements
var b = new Array(3);

// create an array with [2, 4]
var c = new Array(2,4)

Access an Array element

An element can be accessed using index.

For example, if we want to access the 3rd element then by index it will be 2 as index starts from 0.

var a = ['first', 'second', 'third', 'fourth']
var b = a[2] // b variable will be assigned value 'third' as string
console.log(a[1]) // a[1] returns value 'second'

Add element to an array

An element can be added to an array at starting or at ending.

To Add at start we need to use shift Method

For example, if we want to add element at start of an array then

var a = ['first', 'second', 'third', 'fourth']
a.unshift('test') // 'test' will be added at start of array variable a
console.log(a) // prints value ['test', 'first', 'second', 'third', 'fourth']

To Add at the end we need to use push Method

For example, if we want to add element at the end of an array then

var a = ['first', 'second', 'third', 'fourth']
a.push('test') // 'test' will be added at end of array variable a
console.log(a) // prints value ['first', 'second', 'third', 'fourth', 'test']

Remove element of an array

An element can be removed of an array from anywhere.

To Remove at start we need to use shift Method

For example, if we want to remove element at start of an array then

var a = ['first', 'second', 'third', 'fourth']
a.shift() // 'first' will be removed from start of array variable a
console.log(a) // prints value ['second', 'third', 'fourth']

To Remove at the end we need to use pop Method

For example, if we want to remove element at the end of an array then

var a = ['first', 'second', 'third', 'fourth']
a.pop() // 'fourth' will be removed from end of array variable a
console.log(a) // prints value ['first', 'second', 'third']

To Remove by using index and length

splice(index, removeCount)
For example, if we want to remove element at second element of an array then

var a = ['first', 'second', 'third', 'fourth']
a.splice(1, 2) // 'second' and 'fourth' will be removed from array variable a
console.log(a) // prints value ['first', 'fourth', 'third']

Length property

Array length can be found by using length property of Array.

For example,

var a = ['first', 'second', 'third', 'fourth']
console.log(a.length) // prints value 4

// if we want to shorten the array then
a.length = 2 // this will reduce the array length to 2 and removes last two elements of variable a.

Other Methods

Slice:

The slice() method returns a shallow copy of a portion of an array into a new array object. The start and end is passed as parameter to select the portion. The original array will not be modified.

Syntax
slice()
slice(start)
slice(start, end)

For example,

var a = ['first', 'second', 'third', 'fourth']
console.log(a.length) // prints value 4

var b = a.slice() // returns new array with all values of a

var c = a.slice(1) //as end is omited, extracts through the end of the sequence. returns ['second', 'third', 'fourth']

var d = a.slice(1, 3) //returns ['third', 'fourth']

// A negative index can be used, indicating an offset from the end of the sequence.
var e = a.slice(-2, -1) //returns ['third']

join:

The join() method creates and returns a new string by concatenating all of the elements in an array. The element are separated by commas (default seperator) or a specified separator string. If the array has only one item, then that item will be returned without using the separator.

Syntax
join()
join(separator)

For example,

var a = ['how', 'are', 'you']

var b = a.join() // returns new 'how,are,you'

var c = a.join('-') // returns 'how-are-you'

concat:

The concat() method joins two or more arrays and returns a new array.

Syntax
array1.concat(array2, array3, ..., arrayX)

For example,

var a = ['how', 'are', 'you']

var b = a.concat([3,4], [5]) // returns new ['how', 'are', 'you', 3, 4, 5]