A few useful, built-in JavaScript methods

John Long
JL Codes
Published in
3 min readFeb 4, 2018

I wanted to take a moment to more thoroughly jot down a handful of useful JavaScript methods that I have been utilizing to solve code katas.

Art by Noah Bradley

parseInt(string, radix)

parseInt is a useful method that converts a number inside a string into an integer.

The method accepts two parameters, the first being the string itself, the second is the radix. The radix dictates what base number system the number inside the string will be converted into. By default this is set to 10 (since we operate in base 10 most of the time). I tend to not play around with the radix, instead just offering the string to be digested in parseInt.

Here’s an example:

let number = parseInt("7")  
// number = 7

A few additional notes about parseInt

  • Only the first number inside the string is converted into an integer
let number = parseInt("7 10 13")
// number = 7
  • If the first character inside the string can’t be converted into an integer, parseInt returns NaN (Not a Number)
let number = parseInt("hello 7 goodbye")
// number = NaN
let number = parseInt("7 hello goodbye")
// number = 7
  • Decimals will return only the first part of the number (so rounded down)
let number = parseInt("4.97")
// number = 4

.toString(radix)

Similar to parseInt, the method ‘toString( )’ converts a number that is an integer into a string. The radix parameter can also be specified, but does not have to be included. The default radix is also 10 for toString( ).

let number = 7
sNumber = number.toString()
// sNumber = "7"

.join( )

JavaScript’s .join( ) method allows you to smash an array of things into a single string, like so:

let array = ["red", "yellow", "blue"]
array.join()
// returns "red,yellow,blue"

If no argument is given to .join, by default commas will be put in between each item from the array. You can also insert characters you would like to put in between each array item as parameters for .join

let array = ["red", "yellow", "blue"]
array.join(" ")
// returns "red yellow blue"
let array = ["red", "yellow", "blue"]
array.join(" dog ")
// returns "red dog yellow dog blue"

.concat( )

The concat( ) method allows you to merge two or more arrays into a single, new array. The arrays will be added in order, on top of the starting array. This means that the order of items within the arrays are preserved.

The syntax looks like:

let newArray = array1.concat(array2, array3, ...)

Some examples:

// Two Arrays
let array1 = ["left", "right", "left"]
let array2 = ["up", "down", "up"]
let bigArray = array1.concat(array2)
// bigArray returns ["left", "right", "left", "up", "down", "up"]
// Three Arrays
let array1 = ["I", "learned"]
let array2 = ["how", "to", "smash"]
let array3 = ["arrays", "together."]
let giantArray = array1.concat(array2, array3)
// giantArray returns ["I", "learned", "how", "to", "smash", "arrays", "together."]

Then you could use the .join( ) method to form a single string!

singleString = giantArray.join(" ")
// singleString returns "I learned how to smash arrays together."

I found each of these built-in methods useful for solving katas because I could easily change the original data into a form with which I could manipulate it, then mold it back into whatever output type was needed.

I’m happy to have these methods added into my wheelhouse and look forward to solving problems with increased speed in the future!

--

--