Boost your Javascript knowledge

Some String Methods:

indexOf():

This method is used for getting the index number of any string.

We know that the index number starts from 0. the last index number will be total length — 1. example:

let sentence = “boost your js knowledge”;
let indexNumber = sentence.indexOf(“your”);

the result will be : 6

It means the index number started from 0 and from index number 6 the word ‘your’ is started.

lastIndexOf():

This method is used for getting the last index number of a repeated word.

let sentence = “boost your js knowledge and boost your self”;
let indexNumber = sentence.lastIndexOf(“boost”);

the result will be : 29

slice( start, end ):

This method is used for slicing a string. This is like slicing a cake with a knife.

It takes two parameters both indicate the index numbers. the starting position, and the ending position. example :

let fruits = “Apple, Banana, Mango”;
let result = fruits.slice(7, 13);

the result will be: Banana

the word Banana is a slice of index no 7 and 13.

substr( start, length):

This method is similar to the slice function but here the second parameter represents the length.

let fruits = “Apple, Banana, Mango”;

let result = fruits.substr(7, 6); — — Banana

let result = fruits.substr(7); — — Banana, Mango

concat():

This method is used for joining two or more strings.

let text = “Hello” + “ “ + “World!”;
let result= “Hello”.concat(“ “, “World!”);

Some Array Methods:

pop():

This method is used for removing the last element from an array.

It returns the removed element.

let fruits = [“Apple”, “Banana”, “Mango”];

fruits.pop() — — Mango

push():

This method is used for added an element in the last position in an array.

It returns the total number of elements after pushing.

let fruits = [“Apple”, “Banana”, “Mango”];

fruits.push(“pineapple”) — — 4

shift():

This method is used for removing the first element from an array.

It returns the removed element

let fruits = [“Apple”, “Banana”, “Mango”];

fruits.shift() — — Apple

unshift():

This method is used for adding an element in an array in the first position.

It returns the total number of elements after adding an element.

let fruits = [“Apple”, “Banana”, “Mango”];

fruits.unshift(“pineapple”) — — 4

sort():

This method is used for sorting array elements in ascending order like a dictionary. It returns the sorted array.

let alphabet = [ ‘b’, ‘a’, ‘d’, ‘c’]

alphabet.sort() — — [ ‘a’, ‘b’, ‘c’, ‘d’]