JavaScript Built in Methods: Strings
While trying to solve some coding problems this week, I kept running into the same wall — getting stuck/paralyzed with indecisiveness and over thinking. With the help of a peer, I discovered the value of thinking and coding out loud. By asking myself the very simple or redundant questions, I get out of my head and into the drivers seat of problem-solving.
This week, the first problem I took on was a code challenge on Code Wars that asked me to reverse a string. I thought this was going to be very simple, and as soon as I started, I began overthinking the solution. I had to take a step back and I realized that all the answers are out there, just a couple clicks away from my finger tips. I did look up the solution and went down a rabbit hole (so to speak) of built in methods for strings. I’ll quickly go over my solution for the reverse a string problem and then touch on some built in methods for strings in JavaScript.
function solution(string) {
return string.split("").reverse().join("")
}In the code snippet above, we are creating a function called solution that takes in a string as an argument and then reverses it. The line of code where we return the result is doing several things.
Get Stephen Galvan’s stories in your inbox
Join Medium for free to get updates from this writer.
First we are splitting the string; let’s use the word “hello” for example. The .split(“”) takes apart a string and converts it into an array so that our string “hello” turns into [“h”, “e”, “l”, “l”, “o”].
Next we use .reverse() which is a built in array method in javascript, meaning we can only use this on an array and not a string. The reason why it works in this case, is because we have converted our “hello” string into an array. And so [“h”, “e”, “l”, “l”, “o”].reverse() becomes: [“o”, “l”, “l”, “e”, “h”].
Lastly, we implement the built in array method, .join(“”), which takes an array and converts the array into a string, so [“o”, “l”, “l”, “e”, “h”].join(“”) returns the value: “olleh”
So in the above solution to the reverse string problem, we are using one built in string method and two built in array methods to solve the problem. I used to think that using built in methods was cheating or rather that I wouldn’t be allowed to use them during coding interviews, and in some cases you will be asked to solve similar problems without using certain built in methods. However, these methods exist to be used, and so the more you practice and are exposed to built in methods, the more you will get a grasp of some of the fundamentals for whatever languages you use.
For more built in methods, and resources checkout w3school and MDN Web Docs.





