Getting started with some commonly used methods of JavaScript

Ashutosh Biswas
3 min readMay 5, 2021

Arrays.

How to use the sort method?

The method has the following syntax:

someArray.sort([compareFunction])

Omitting the compareFunction triggers default behavior which we will see at the end of this section. First let’s see how to use compareFunction because it is cool!

compareFunction has two parameters: they represents any two arbitrary non-undefined elements of someArray of different indices(all undefined elements are sorted to the end of the array, with no call to compareFunction). sort method calls compareFunction internally and uses it’s return value as a cue that indicates where to put the compareFunctions arguments to sort the array. The compareFunction should return a number. This number gives cues to sort method the following way:

  • A negative number says: put the 1st argument before the 2nd argument
  • A positive number says: put the 1st argument after the 2nd argument
  • Zero says: don’t modify their relative position

Let’s see an example:

[9, 100, 3, 2, 8].sort((x, y) => {
if (x < y) return -1; // say sort to put x before y
if (x > y) return 1; // say sort to put x after y
return 0; // say sort to put
// x before y if x is before y
// and x after y if x is after y
});
// [ 2, 3, 8, 9, 100 ]

When compareFunction is omitted, all non-undefined array elements are sorted by converting them to strings and comparing strings in UTF-16 code units order. All undefined elements are put to the end of the array.

An super brief introduction to reduce

reduce is used to get a single value from an array. For example you may want to the get the sum of all numbers in an array, then you can use reduce:

// find the sum of an array of numbers
[10, 20, 30].reduce((a, b) => a + b)

If that makes no sense, that’s perfectly fine. For the detail, go to MDN(It’s worth reading!).

find method: Let’s find an element

If you need to find an array element by some criteria, this is find is your friend. Just give a function. find calls that function for each element by giving that element as it’s first argument until that function returns true.

So you can use it like below to find find first odd number in the following array:

[2,4,5,8,7].find(e => e % 2)  // 5

findIndex method

It’s similar to the find method. It gives you the index of the element rather than the element itself. Sometimes it’s very handy.

[2,4,5,8,7].find(e => e % 2)  // 2

reverse method

It reverses an array in place:

let a = [1, 2, 3];
a.reverse();
console.log(a);
// [3, 2, 1]

If you want to get a new copy, then you can do the following trick:

[...a].reverse()

Time for some math!

Math.abs(n)

It’s so simple that you implement it yourself it 5 seconds:

const abs = n => n < 0 ? -n : n;

That’s all there is to know about Math.abs.

Math.random()

Finding the utility to get random values is a vital thing to a programming language. We are so helpless without it. Because it’s not easy to find random numbers. And this method doesn’t actually give us a true random number. But what it gives us is just enough for most use cases.

This method returns a random real number in the range including 0 and up to 1(excluding 1).

Fun fact: I don’t see any chance to get a zero. It just gives me at least something. This method is so big-hearted!

Strings methods

replace method: find and replace

The most simplest example is like below:

"Hello user".replace("user", "Alien")
// "Hello Alien"

You can also find text using regular expressions:

"Hello Obama".replace(/\w+$/, "Alien")
// "Hello Alien"

Here we just scratched he surface of this method. There are many more to discover which are beyond the scope of this article.

substr method

This method is like a camera to take photos of strings. You need to focus it to your string to the specific part and press the shutter button, and it will return a exact copy of it:

"Yellow".substr(1)  // means from index 1 to rest
// "ellow"
"JavaScript".substr(1,4) // means from index 1(including) and next 3 characters
// avaS

split method

This method is very common and use full. It splits a string into an array. It takes a argument where you tell it where to do the spiting:

"Jhankar Mahbub".split(" ")
// ["Jhankar", "Mahbub"]

The argument can also be regular expression:

"Somewhere-over_the-rainbow".split(/[-_]/)
["Somewhere", "over", "the", "rainbow"]

--

--