Some less-used javaScript methods

Sa'ad
3 min readMay 5, 2021

Hey, this my first tutorial in the medium. In this tutorial, I’ll introduce you to ten javascript methods that seem to me are useful, but we are not very much known with these methods. Let’s get started without lengthening the introduction.

  1. The first method is a string method. Which is includes() method. With this method, you can search a string whether it included in a string or not. If it is, it will return true else false. Here is an example below, hope this will help you to understand the method.

Example :

const first = “this is the first string” ;

const second = “is” ;

console.log(`the word “${second}” ${first.includes(second) ? ‘included’ : ‘not included’} inside the first`)

// ans… the word “is” included inside the first

2. The second method is also a string method, which is the replace() method. With this method, you can replace a word or a char that is inside a string with your specified word or char.

Example:

const sentence = “this is a sentence”

console.log(sentence.replace(“sentence”, “text”))

// ans… this is a text

3. The third method is a mathematical method. Which is the floor() method. Using this method you can convert a floating-point number to an integer number, which is the largest integer less than or equal to a provided number.

Example:

console.log(Math.floor(5.05555))

// ans… 5

4. The fourth method also a mathematical method. Which is ceil() method. Using this method you can convert a floating-point number to the next largest integer which is up to the given floating number.

Example :

console.log(Math.ceil(5.05555))

// ans… 6

5. The fifth method also a mathematical method. Which is round() method. Using this method you can convert a given floating-point number as an argument to the nearest integer to it.

Example:

console.log(Math.round(5.999995))

// ans… 6

6. The sixth method is also a mathematical method. Which is max() and min() method. The first one will return you the largest value among the given arguments. And the second one will return you the lowest value among the given arguments.

Example:

console.log(Math.max(2, 3, 4))

// ans… 4

console.log(Math.min(2, 3, 4))

// ans… 2

7. The seventh method is an array method. Which is concat() method. Using this method you can merge two or more arrays. And it will return you a new array. But the incredible thing is this method does not change the existing arrays

Example:

const arr1 = [1, 2, 3, 4]

const arr2 = [5, 6, 7]

console.log(arr1.concat(arr2))

// ans… [1, 2, 3, 4, 5, 6, 7]

8. The eighth method is forEach() method. Using this method you can apply a given function once for each element of the array.

Example:

const arr = [5, 8, 9, 4, 6, 3]

arr.forEach((element) => console.log(element > 5));

// ans… false true true false true false

9. The ninth method is join() method. This method will return a new string by concatenating all of the array elements also separates the elements by commas or a specified separator.

Example:

const arr1 = [“how”, “are”, “you”]

console.log(arr1.join());

console.log(arr1.join(‘ ‘));

console.log(arr1.join(‘-’));

// ans… “how,are,you”

// ans…”how are you”

// ans…”how-are-you”

10. The last method is indexOf() method. You can find out the first index of an array element using this method.

Example:

const arr = [“find” , “index”, “of”, “an”, “element”];

console.log(arr.indexOf(“element”))

// ans…4

--

--