JavaScript Array methods introduced in ECMAScript 1

JavaScript Array methods introduced in ECMAScript 1

Javascript Array methods es1

Array.concat()

The concat() method is used to join two or more arrays. This method does not change the existing arrays, but returns a new array with contains the value of merged arrays.

Syntax:

const new_array = array_1.concat(array_2, array_3, ..., array_n);

Example:

const one = ["a", "b"];
const two = ["c", "d"];
const three = ["e", "f"];
const result = one.concat(two, three);
console.log(result); // Output: ["a", "b", "c", "d", "e", "f"]

Array.join()

The join() methods create a new string by concatenating all of the elements of in an array. The elements in the array will be separated by separator. The default separator is comma(,). This method does not change the original array.

Syntax:

const str = array.join(separator);

Example:

const nameArray = ["p", "r", "a", "s", "e", "n", "j", "i", "t"];
console.log(nameArray.join()); // Output: "p,r,a,s,e,n,j,i,t"
console.log(nameArray.join("")); // Output: "prasenjit"
console.log(nameArray.join("-")); // Output: "p-r-a-s-e-n-j-i-t"


Array.pop()

The pop() method removes the last element of an array and return the deleted element. This method changes the length of the array.

Syntax: 

array.pop()

Example:

const fullName = ["Shivendra", "Singh", "Tomar"];
const lastName = fullName.pop();
console.log(fullName); // Output: ["Shivendra", "Singh"]
console.log(lastName); // Output: "Tomar"

Array.push()

The push() method adds the elements to the end of an array and returns the length of the new array.

Syntax:

array.push(element_1, element_2, ..., element_n)

Example:

const name = ["p", "r", "a", "s", "e", "n"];
const count = name.push("j", "i", "t");
console.log(name); // Output: ["p", "r", "a", "s", "e", "n", "j", "i", "t"]
console.log(count); // Output: 9

Array.shift()

The shift() method removes the first element of an array and returns the removed element. In case if empty array it returns undefined. 

Syntax:
array.shift()

Example:
const nameArray = ["p", "r", "a", "s", "e", "n", "j", "i", "t"];
const firstElement = nameArray.shift();
console.log(nameArray);
// Output: ["r", "a", "s", "e", "n", "j", "i", "t"]
console.log(firstElement);
// Output: "p"

Array.unshift()

The unshift() method adds the elements to the beginning of an array and returns the length of the updated new array.

Syntax:
 array.unshift(element_1, element_2, ..., element_n)

Exmaple:
const nameArray = ["t", "i", "k", "a"];
console.log(nameArray.unshift("p", "r", "a"));
// Output: 7
console.log(nameArray);
// Output: ["p", "r", "a", "t", "i", "k", "a"]

Array.slice()

The slice() method returns the portion of an array selected by start to end index (end not included) into a new array. This method does not change the original array.

Syntax:
array.slice(start, end)

Parameters:
start: optional (index from where we want to extract the array)
end: optional (index to extract the array)

Example:
const name = ["p", "r", "a", "t", "i", "k", "a"];
console.log(name.sllice(4))
// output: ["i", "k", "a"]
console.log(name.sllice(2, 4))
// output: ["a", "t", "i"]
console.log(name.sllice(4, 6))
// output: ["i", "k"]

Array.splice()

The splice() method removes and replace the existing content of an array at given from index. This method changes the original array and returns the removed elements into a new array.

Syntax:
array.splice(start, deleteCount, item_1, item_2, ..., item_n)

Example:
const days = ["Mon", "Wed", "Thu", "Sat"];
// Inserts at index 1
days.splice(1, 0, "Tue");
console.log(days);
// Output: ["Mon", "Tue", "Wed", "Thu", "Sat"]
// Replace at index 4
days.splice(4, 1, "Fri")
console.log(days)
// Output: ["Mon", "Tue", "Wed", "Thu", "Fri"]

Array.toString()

The toString() method returns a string which contains element of an array.

Syntax:
const str = array.toString()

Example:
const nameArray = ["p", "r", "a", "s", "e", "n", "j", "i", "t"];
console.log(nameArray.toString())
// Output: "p,r,a,s,e,n,j,i,t"

Array.reverse()

The reverse() method reverses an array elements and returns into a new array. This method changes the original array.

Syntax:
array.reverse()

Example:
const nameArray = ["p", "r", "a", "s", "e", "n", "j", "i", "t"];
const newArray = nameArray.reverse();
console.log(nameArray);
// Output: ["t", "i", "j", "n", "e", "s", "a", "r", "p"]
console.log(newArray);
// Output: ["t", "i", "j", "n", "e", "s", "a", "r", "p"]

Array.sort()

The sort() method sort the element of an array in place and returns the sorted array into a new array.

Syntax:
array.sort()

Example:
const days = ['Sun', 'Mon', 'Tue', 'Wed'];
const sortedDays = days.sort();
console.log(days);
// Output: ["Mon", "Sun", "Tue", "Wed"]
console.log(sortedDays);
// Output: ["Mon", "Sun", "Tue", "Wed"]

Array.valueOf()

The valueOf() is the default method of an array this method returns the same array and does not change the original array.

Syntax:
array.valueOf()

Example:
const days = ['Sun', 'Mon', 'Tue', 'Wed'];
console.log(days.valueOf());
// Output: ["Sun", "Mon", "Tue", "Wed"]






Comments

Popular posts from this blog

Top 5 Programming Languages to Learn in 2020

10 NodeJS Tips and Tricks for developers

What is Deno and will it Replace NodeJS?