Important Javascript methods you need to know

Mainul Fahim
2 min readMay 5, 2021

Array Methods

1.Shift()

- This method is used to remove the first element from an array.While pop() method removes the last element.

Example : let items=[‘first’,’second’,’third’];

items.shift();

console.log(items); //output : items=[‘second’,’third’]

2. Unshift()

  • This method is used to add a new element at the beginning of an array.While push() adds at the end of an array.

Example : let items=[‘second’,’third’];

items.unshift(‘first’);

console.log(items); //output : items=[‘first’‘second’,’third’]

3. Reverse()

-This method is used to reverse the order of the elements in an array.

Example : let items=[‘first’,’second’,’third’];

items.reverse();

console.log(items); //output : items=[’third’,’second’,’first’]

4. Slice()

-This method is used to cut a certain portion of an array without changing the original array.It returns a new array with the sliced elements.

Example : let items=[‘first’,’second’,’third’,’four’,’five’];

const slicedItems= items.slice(1,3);

console.log(slicedItems); //output : items=[’second’,’third’]

console.log(items); //output : items=[‘first’,’second’,’third’,’four’,’five’]

String methods

  1. trim()

-this method is used to remove whitespaces from the beginning and ending of a string.

Example : const word=’’ Hey MR Iron Man! “;

console.log(word.trim()); //output : ‘’Hey MR Iron Man!”

  1. indexOf()

-this method is used to get the index of a certain searched value from a string.It takes the first occurance of the searched value.

  1. charAt()

- this method is used to get an individual character from a string.

Example : const animal=’’dog’’;

console.log(animal.charAt(2)); // output : “a”

Math methods

  1. Math.round()
  • This method returns the value of the given number rounded to the nearest integer.

Example : console.log(Math.round(.9)); //output 1

2.Math.sqrt()

  • This method returns the positive square root of the given number

Example : console.log(Math.sqrt(49)); //output 7

3.Math.max() and Math.min()

-these methods return the maximum and minimum number from an array.

Example : let array=[1,2,3,4,5];

console.log(array.Math.max()); //output 5

console.log(array.Math.min()); //output 1

--

--