String Methods are pre-loaded methods that can be used against string primitives
or objects to alter values or return data. I spent some time looking into a few
of these useful methods and will share some useful information and examples below.
Hopefully, you will find this helpful in your journey to master JavaScript!
Concatenation Method - concat()
The first method I would like to share is the Concatenation Method. Not only is this
a fun word to say, but it can also be very useful in manipulating strings. This
method works against an existing string by passing in a second string (or multiple)
and it returns a new string that consists of the characters of the original string
followed by the characters of all of the passed in strings. If you are working with
multiple small strings and want to combine them all together into one complete
string (e.g., a full address instead of separate street, city, state, etc.) you can
invoke the concat() method to add the latter sections of the desired string to the
former.
const firstString = "Willem";
const secondString = "Dafoe";
const concatString = firstString.concat(" ", secondString);
console.log(concatString);
//logs the concatenated string as "Willem Dafoe"
String Split Method - split()
The opposite of the concatenation method could be considered the Split Method. This
method takes an existing string and splits it into an array of substrings. The Split Method
accepts two optional parameters: a separator and a limiter. The separator is a string
or character that the function uses to separate the new substrings. The separator is
not included in the substrings. The limiter is an integer that limits the number of
times the original string will be split. When a limiter is used, all characters following
the final split are not included in the produced array. This method can be useful when you
receive a complex string with multiple chunks of information that you would like to handle
individually.
const beatles = "John, Paul, George, Ringo";
const individualBeatles = beatles.split(", ");
console.log(individualBeatles);
//logs an array with each Beatle represented individually
Uppercase Method - toUpperCase()
The final string method I researched was the Uppercase Method. This method does not
require any additional parameters and returns a new string that is identical to the
original apart from having all its characters replaced by their uppercase counterparts.
There are many times where capitalizing strings can be useful. Strings can be more easily
compared when capitalization is consistent. By using the Uppercase Method on multiple
strings, you do not have to worry about any case inconsistencies.
const lowerCase = "this is a string";
const upperCase = lowerCase.toUpperCase();
console.log(upperCase);
//logs upperCase as "THIS IS A STRING"