Javascript Built-In Methods

Harriet Bicknell
Webtips
Published in
3 min readAug 16, 2020

In the past few years, JavaScript has come out with many new built-in methods and functions which have made JavaScript more readable, efficient and use way less code. I am going to go over some of these ready-made methods that I find myself using time and time again to manipulate data.

String Methods

In JavaScript, a string stores a series of characters/text inside double or single quotes. For the following methods, let's go with the classic example string,const str = "Hello World!"to demonstrate.

str.concat()

This method joins the text of 2 strings and returns a new string:

let secondString = "!!"
str.concat(secondString) //=> "Hello World!!!"

str.repeat()

The clue is in the name, it allows you to repeat a string!

str.repeat(3) //=> "Hello World!Hello World!Hello World!"

str.split()

Splits a string into an array of substrings by the separator you decide. In the first instance, I will separate the string by character. In the second instance, I will separate the string by space!

str.split('') //=> ["H", "e", "l", "l", "o", " ", "W", "o", "r", "l", "d", "!"]str.split(' ') //=> ["Wello", "World!"]

str.toLowerCase()

Converts the string to lowercase.

str.toUpperCase(' ') //=> "hello world!"

You can also you str.toLocaleLowerCase() which respects the string’s locale.

str.toUpperCase()

Converts the string to uppercase.

str.toUpperCase(' ') //=> "HELLO WORLD!"

You can also you str.toLocaleUpperCase() which respects the string’s locale.

str.includes()

This returns a Boolean if the value entered is present in the string

str.includes('!') //=> true
str.includes(' ') //=> true
str.includes('Z') //=> false

Array Methods

In JavaScript, an array is a collection of elements that are stored in a single variable. Arrays are denoted by square brackets and each element is separated by a comma, eg. arr = ['a', 1, ‘b', 2]. Every element in an array has an index number, which refers to its position in the array (starting at 0), which allows us to access a specific element using its index, eg. arr[0] //=> "a".

This is the example array: const arr = ['one', 2, ‘three', 4].

arr.join()

Joins all elements of an array into a string by the joiner you decide

arr.join('!') //=> "one!2!three!4"arr.join(' ') //=> "one 2 three 4"arr.join('') //=>"one2three4"

arr.concat()

This returns a new array comprised of multiple arrays/values joined together.

arr.concat("five") //=> ["one", 2, "three", 4, "five"]arr.concat(["five", 6]) //=>["one", 2, "three", 4, "five", 6]

arr.indexOf()

This returns the first index of an element in the array that is equal to the value you have specified. If none is found, it will return -1

arr.indexOf("7") //=> -1arr.indexOf("one") //=> 0arr.indexOf("three") //=> 2

arr.map()

This will iterate through your array, perform the function you provided on every element in the array and return a new array with the results.

arr.map(el => el === 2) //=>  [false, true, false, false][1,2,3,4].map(el => el + 10) //=> [11, 12, 13, 14]

arr.sort()

This will iterate through your array, sort the array either alphabetically or numerically, and ascendingly (up) or descendingly (down).

By default, the sort() method sorts the values as strings in alphabetical and ascending order.

arr.sort() //=> [2, 4, "one", "three"][7,2,50,20].sort() //=>  [2, 20, 50, 7]

arr.filter()

Creates a new array with all of the elements of this array for which the provided function returns true.

arr.filter(el=> el < 10) //=> [2, 4]

So there you go! A few methods to help simplify your code when dealing with arrays and strings!

This is not a comprehensive list of methods so please refer to MDN documentation or w3schools.com for more information!

--

--