I Recently Discovered Some Helpful Javascript Methods

Andrew Smoker
3 min readJul 22, 2021

During my time looking for a Software Engineering position, I quickly realized I needed to improve my algorithm skills for technical assessments and interviews. I joined a club of former Flatiron Students who all wanted to do the same and it has been extremely helpful. Not only do we talk about solutions, but we also discuss the process of getting there. For me, it has been extremely helpful to see how other people think about a problem and what tools they use to solve it.

Throughout that process I’ve discovered a number of Javascript methods that I had never used before because they never came up in school. However, they can be incredibly useful and are definitely a sleek way to write solutions to problems. I want to share a few of them because they really have been fun to learn!

padStart()

One of the algorithms I worked through was to take a string of credit card numbers and mask out all but the last four digits. Initially my thought was to do a loop through the string and push “#” into a new string for each index until I got to the last four indices when I would instead push the actual character. This solution proved to be valid, but in comparing solutions, someone brought up padStart. padStart() accepts two arguments, the target length of the string and what you want to add to get to that length. I could write a one line solution to the credit card problem using this!

//examplelet creditCard = "1234567898"//first I slice out the last four digits of the string to keep them in place.  creditCard.slice(-4).padStart(creditCard.length, "#")//then I call padStart and pass in the original length of the creditCard string and "#" as the second argument.  This will take the last four digits I sliced and add "#" as many times as needed to get back to the original length.//output: "######7898".  //The output is a string the same length as the original with the last four digits in place.  Pretty sweet!

There is also a padEnd() method that does the same except adds to the end of the string instead of the beginning. A nifty method that could come in handy given the right situation!

Date.parse()

Another algorithm I worked on required comparing two strings of dates (“January 1, 2015” and “January 15, 2015”) to make sure the first one was before the second one. We cannot compare the two strings for date value in that format though. This is where I discovered Date.parse() which converts a string representation of a date into the number of milliseconds since January 1, 1970. If we parse both dates, we can compare the milliseconds to make sure the first is less! A much better solution than breaking down the string to compare years, dates and months.

let dateOne = "January 1, 2015"
let dateTwo = "January 15, 2015"
console.log(Date.parse(dateOne))
// logs 1420088400000
console.log(Date.parse(dateTwo))
//logs 1421298000000
//Now we can compare those two numbers and get our solution!return (Date.parse(dateOne) < Date.parse(dateTwo))
//returns true

.match()

The final method I want to bring up is .match(). This method accepts a regular expression argument and checks the string for matches. However, this just returns the first match, not all the matches. If we want to return every match, all we need to do is simply add a g to the end of our regex. This stands for global and checks for ALL matches. If we need to know the number of all the matches we can just add.length and return the count of each match in our array!

let string = "aaba"
console.log(string.match(/a/))
//logs ["a", index: 0, input: "aaa"]
console.log(string.match(/a/g))
//logs ["a", "a", "a"]

These may seem like basic operations, but as I discover new methods that simplify tasks I add another tool to my toolbox that I can use in the future. I love continuing to learn about these simple, yet helpful ways to write code!

--

--

Andrew Smoker

I am 34 years old and making a huge career change by attending Flatiron School’s Software Engineering Bootcamp. Excited to learn!