A few java script method

Md. Tareq Hasan Chowdhury
2 min readMay 10, 2021

1. String.prototype.indexOf()
This method is used to find out the index of an element in a string. It gives you the position of the search element or first matched element. If the element is not found it will return -1.

Syntax: string.indexOf(searchvalue, start)

The first argument searchValue defines that the value you want to be searched in the base string. Second argument defines the starting index from where the searchValue is to be searched.

Example:

var str= “welcome to Bangladesh”

var r=str.indexOf(“c”);

Output: 3

2. String.prototype.lastIndexof()

This method is similar to indexOf() method with the difference that it starts searching an element from the last position of the base string.

Example:

const str= “welcome to Bangladesh”

const r=str.indexOf(“e”);

Output: 18

3. String.prototype.concat()

This method combines specified string at the end of this string. It returns combined string like appending another string.

Example:

const str1=“Hello”;

const str2= “World”;

console.log(str1.concat(str2));

Output: Hello World

4. String.prototype.slice()

This method is used to fetch the part of the string and returns new string. It required to specify the index number as starts and end parameters to fetch the part of the string.

This method also allows to pass a negative number as index. In such case this method start fetching from the end.

Syntax: string.slice(start,end)

start- It defines the index where the fetching start.

end- It is optional and it defines the index where the fetching end.

Example:

const str= “Bangladesh”;

console.log(str1.slice(5));

Output: adesh

console.log(str1.slice(-3));

Output: esh

console.log(str1.slice(2,5));

Output: ngla

5. String.prototype.toLowerCase()

This method is used to convert the string to lower case. That means it covert the base string from capital to small letter.

Example:

const str=“welcome to bangladesh”

console.log(str.toLowerCase());

Output: WELCOME TO BANGLADESH

6. String.prototype.toUpperCase()

This method is used to convert the string to upper case. That means it covert the base string from small to capital letter.

Example:

const str=“WELCOME TO BANGLADESH”

console.log(str.toUpperCase());

Output: welcome to bangladesh

7. array.prototype.every()

This method is used to check the all elements in the given array to satisfying the provided condition. If the array element satisfying the condition it returns true otherwise false.

Example:

const arr=[22,13,34,45,49,28];

const numbers=(number)=> number<50;

console.log(arr.every(numbers));

Output: true

8. array.prototype.join()

This method is used to join the array elements into a string. This elements of the string separate by a specified separator. If no parameter will not be passed into this method then the default value will be comma(,).

Example:

const str=[‘apple’, ‘mango’, ‘pear’];

console.log(str.join())

Output: apple, mango, pear

console.log(str.join(“-”))

Output: apple-mango-pear

9. array.prototype.shift()

This method is used to removes the first element of the base array. If the base array is empty it returns undefined. Otherwise it returns removed element.

Example:

const str=[‘apple’, ‘mango’, ‘pear’];

const result=str.shift();

console.log(result);

Output: apple

console.log(str);

Output: [‘mango’, ‘pear’];

10. array.prototype.unshift()

This method is used to adds one or more new element at the beginning of the base array. Then it returns the new length of the base array.

Example:

const str=[‘apple’, ‘mango’, ‘pear’];

const result=str.unshift(‘orange’, ‘banana’);

console.log(str);

Output: [‘orange’, ‘banana’, ‘mango’, ‘pear’];

--

--