Some JavaScript Methods you should know

Oct 20, 2021
4 min read
720 words

In this article, we are gonna look at some JavaScript methods that you should know. You might know some as well if yes then feel free to skip that particular section. So let's get into it.

concat(..)

This method can be used in Array as well as in strings. this joins the two variables or parts together if they belong to the same data type.

script.js

let a = "foo";
let b = ["f", "o", "o"];
 
let c = a.concat("bar"); // "foobar"
let d = b.concat(["b", "a", "r"]); // ["f","o","o","b","a","r"]

Since it doesn't' not change the actual variable so we always need to store the value returns from concat for example -

script.js

console.log(a); // "foo"
console.log(b); // [ 'f', 'o', 'o' ]
console.log(c); // "foobar"
console.log(d); // [ 'f', 'o', 'o', 'b', 'a', 'r' ]

reverse()

Arrays have a reverse() in-place mutator method, but strings do not so we can only use it in arrays and not for strings.

script.js

let a = "foo";
let b = ["f", "o", "o", "!"];
 
a.reverse; // undefined
b.reverse(); // ["!","o","O","f"]
console.log(b); // ["!","o","O","f"]

Since this method cannot reverse string so the question is how we can reverse sting? The answer is simple we have to make a function for it.

script.js

let a = "foo";
 
// function for reversing a string
function reverseString(str) {
  return str.split("").reverse().join("");
}
 
console.log(a); // "foo"
 
let b = reverseString(a);
console.log(b); // "oof"

reverseString() method returns the value so we need to store that in the variable and voila it works. In the above example, we are just taking a string and splitting it into an array then we use the reverse() and join() back together.

split(..)

In the previous section we used the split() method now let's see how it actually works.

script.js

let a = "foo";
let arr = a.split("");
 
console.log(a); // foo
console.log(arr); //  [ 'f', 'o', 'o' ]

split() method returns an array and it splits based on what you passed as the argument in that. For example:

join(..)

In the the previous example we used the join() as well it was for to convert the array into the string.

script.js

let a = ["f", "o", "o", "!"];
let str = a.join("");
 
console.log(a); // [ 'f', 'o', 'o', '!' ]
console.log(str); //  foo!

toExponential()

This method converts the value to the exponential as we can already understand by name of it. Let's see how we can implement it.

script.js

let a = 5e7;
console.log(a); // 50000000
let b = a.toExponential();
console.log(b); // 5e+7

Now, what if we use some mathematical operations in it. let's see.

script.js

var c = b * b;
console.log(c); // 2.5e+21
var d = 1 / b;
console.log(d); // 2e-11

toFixed(..)

toFixed(..) method allows you to specify how many fractional decimal places you’d like the value to be represented with:

script.js

let a = 42.59;
a.toFixed(0); // "43"
a.toFixed(1); // "42.6"
a.toFixed(2); // "42.59"
a.toFixed(3); // "42.590"
a.toFixed(4); // "42.5900"

toPrecision(..)

toPrecision(..) is similar, but specifies how many significant digits should be used to represent the value:

script.js

var a = 42.59;
a.toPrecision(1); // "4e+1"
a.toPrecision(2); // "43"
a.toPrecision(3); // "42.6"
a.toPrecision(4); // "42.59"
a.toPrecision(5); // "42.590"

Some other example -

script.js

42.toFixed( 3 ); // SyntaxError
 
// these are all valid:
(42).toFixed( 3 ); // "42.000"
0.42.toFixed( 3 ); // "0.420"
42..toFixed( 3 ); // "42.000"

42.toFixed(3) is invalid syntax, because the . is swallowed up as part of the 42. literal (which is valid—see above!), and so then there’s no. property operator present to make the .toFixed access.

42..toFixed(3) works because the first . is part of the number and the second . is the property operator. But it probably looks strange, and indeed it’s very rare to see something like that in actual Java‐Script code.

Wrapping up

There are a bunch more methods that I have not covered such as length, indexOf, toUpperCase, and so on. Maybe we will cover those in the future.

Jatin's Newsletter

I write monthly Tech, Web Development and chrome extension that will improve your productivity. Trust me, I won't spam you.

Share on Social Media: