String methods
We have worked with strings already and chances are that you have run into some of the methods on strings by now. There are a few we didn't address specifically just yet and we are going to discuss them in this section.
Combining strings
When you want to concatenate strings, you can use the concat()
method. This does not change the original string(s); it returns the combined result as a string. You will have to capture the result in a new variable, else it will get lost:
let s1 = "Hello ";
let s2 = "JavaScript";
let result = s1.concat(s2);
console.log(result);
This will log:
Hello JavaScript
Converting a string to an array
With the split()
method we can convert a string to an array. Again, we will have to capture the result; it is not changing the original string. Let's use the previous result containing Hello JavaScript
. We will have to tell the split
method on what string it should split. Every time it encounters...