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.
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.
#javascript-tips #javascript #javascript-development