What’s the use of the method in JavaScript?

method in JavaScript

  • Concat ( ) Method
  • Join ( ) Method
  • Reverse ( ) Method
  • Slice( ) Method
  • Splice ( ) Method
  • toString( ) Method
  • Array.isArray ( ) Method
  • IndexOf ( ) Method
  • Fill ( ) Method
  • unshift ( ) Method
  • Shift( ) Method
  • Shift( ) Method
  • pop( ) Method
  • Map ( ) Method

Concat ( ) Method

The concat() method is used to merge two or more arrays. This method does not change
the existing arrays, but instead returns a new array.

Syntax:- new_array = old_array.concat(value1, value2, value_n);
Syntax:- new_array = old_array1.concat(old_array2, old_array_n);

Join ( ) Method

The join() method joins the elements of an array into a string, and returns the string. The elements will be separated by a specified separator. The default separator is the comma (,).

Syntax: – array_name.join(separator);

Reverse ( ) Method

The reverse() method reverses the order of the elements in an array.

Syntax :- array_name.reverse( );

Ex:- dev.reverse( );

Slice( ) Method

The slice( ) method returns a shallow copy of a portion of an array into a new array object selected from beginning to
end (end not included). The original array will not be modified.

Syntax: – array_name.slice(start, end)

Start
If begin is undefined, the slice begins from index 0.
If begin is greater than the length of the sequence, an empty array is returned.
A negative index can be used, indicating an offset from the end of the sequence. slice(-2) extracts the last two
elements in the sequence.

End
If end is omitted, slice extracts through the end of the sequence (arr.length).
If end is greater than the length of the sequence, slice extracts through to the end of the sequence (arr.length).
A negative index can be used, indicating an offset from the end of the sequence. slice(2,-1) extracts the third
element through the second-to-last element in the sequence.
slice extracts up to but not including end.

Splice ( ) Method

The splice() method changes the contents of an array by removing existing elements and/or adding new elements. This method changes the original array.

Syntax:- array_name.splice (start, deletecount, replacevalues);

Start – The first argument start specifies at what position to add/remove items, uses negative values to specify the position from the end of the array.

Deletecount – The second argument deletecount, is the number of elements to delete
beginning with index start.

Replacevalues – replacevalues are inserted in the place of the deleted elements. If more
than one separate it by a comma.

toString( ) Method

The toString ( ) method returns a string containing the comma-separated values of the array. This method is invoked automatically when you print an array. It is equivalent to invoking the join ( ) method without any arguments. The
the returned string will separate the elements in the array with commas.

Syntax: – array_name.toString();

Array.isArray ( ) Method

The Array.isArray() method determines whether the passed value is an Array. This function returns true if the object is an array, and false if not.

Syntax:- Array.isArray(value);

IndexOf ( ) Method

This method allows us to easily find the occurrence of an item in an array.

  • If the item is not found, it returns -1.
  • The search will start at the specified position, or at the beginning if no start position is specified, and end the search at the end of the array.
  • If the item is present more than once, the indexOf method returns the position of the first occurrence.

Syntax: – var position = array_name.indexOf(item, start);

Ex:- var position = dev.indexOf(“Rohit”, 2);

Fill ( ) Method

The fill() method fills all the elements in an array with a static value.

Syntax:- array_name.fill(value, start, end)

unshift ( ) Method

The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.
This method changes the length of an array.

Syntax: –
Array_name.unshift(value1, value2, value_n);

Push ( ) Method

The push() method adds one or more elements to the end of an array and returns the new length of the array.
The new item will be added at the end of the array.
This method changes the length of the array.

Syntax: –
Array_name.push(value1, value2, value_n);

Shift( ) Method

The shift() method removes the first element from an array and returns that removed element. This method changes the length of the array.

Syntax: – array_name.shift( );

Ex: – geek.shift( );

pop( ) Method

The pop() method removes the last element from an array and returns that removed element. This method changes the length of the array.

Syntax: – array_name.pop( );

Ex: – geek.pop( );

Map ( ) Method

The map() method creates a new array with the results of calling a provided function on every element in the calling array.
The map() method calls the provided function once for each element in an array, in order.

map() does not execute the function for array elements without values.
map() does not change the original array.

Syntax:- array_name.map(function(currentValue, index, array)
{
}, thisValue

Tagged : / / / /
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x