Good Parts of JavaScript — Methods From the Standard Library

Spread the love

JavaScript is one of the most popular programming languages in the world. It can do a lot and have some features that are ahead of many other languages.

In this article, we’ll look at some useful array methods from the JavaScript standard library.


The JavaScript standard library has many useful array methods.


The unshift adds items to the beginning of the array.

It returns the array’s new length.

For instance, we can write:

const a = ['a', 'b', 'c'];
const b = a.unshift('d');

Then a becomes [“d”, “a”, “b”, “c”] and b is 4.


If we have to work with traditional functions, then these methods would help us.

function.apply(thisArg, argArray)

We can use apply to call a traditional function with a different value of this than what’s inside with an array of arguments.

For instance, we can write:

function foo(a, b) {
  console.log(`${this.value} ${a} ${b}`);

  value: 'foo'
}, ['bar', 'baz']);

Then we get:

foo bar baz

from the console log output because this is:

  value: 'foo'

a is 'bar' and b is 'baz' .


Various number methods also make our lives easier when we work with numbers.


The toExponential method lets us convert numbers to exponential notation with the number of fractional digits after the decimal point.

The returned value is a string.

For instance, we can write:


We get:


from the console log output.


The toFixed method returns a string representation of a number in decimal form.

It’ll have the number of digits after the decimal point as we specify them.

For instance, we can write:


And we get:


from the console log.


The toPrecision method converts a number to a string in decimal form.

The precision parameter is optional and controls the number of digits of precision.

For instance, we can write:


And get:



toString converts number to a string.

We can specify the optional radix parameter to control the base of the number.

radix should be between 2 and 36. The default radix is 10.

For instance, we can write:


to convert Math.PI to base 8. We get:



The Object.prototype has some useful methods that we can use.


hasOwnProperty lets us check if a property of an object is inherited or own property.

true is returned if name is an own property.

For instance, we can write:

const obj = {
  foo: 'bar'
const child = Object.create(obj);

Then we get false since foo is an inherited property of child .


The RegExp constructor also comes with some useful methods for matching patterns.


The exec method finds all the matches of a regex pattern in a given string.

For instance, we can write:

console.log(/foo/g.exec('foo foo bar'));

Then we get the matches in an iterator.


The test method is used for checking if a string matches the given regex.

For instance, we can write:

console.log(/foo/g.test('foo foo bar'));

And get true logged because 'foo' is in ‘foo foo bar’ .


Various string methods are also very useful for manipulating strings.


The charAt method returns the character at the given position.

For instance, we can write:

const name = 'joe';
const initial = name.charAt(0);

and get 'j' as the value of initial .


charCodeAt is similar to charAt except that the integer representation of the code point of the character is returned instead of the character itself.

For instance, we can write:

const name = 'joe';
const code = name.charCodeAt(0);

Then we get code 106 for character 'j' .

If pos is less than 0 or bigger than or equal to string.length then NaN is returned.


We can use the concat method to concatenate one or more strings together.

For instance, we can write:

console.log('foo'.concat('bar', 'baz'));

and get foobarbaz from the console log output.


There’re many array, strings, and regex methods that we can’t overlook.

They’re very useful and let us deal with any kind of data with ease.

Combining strings with concat is useful.

We can use the regex test method to check if a string match a regex pattern, and do much more.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *