Strings
Strings in JavaScript are arrays of characters. You define them using single or double quotes, or backticks:
"Hi there";
"Hi there";
Bothe double quoted and single strings allow escape characters:
"Hi \\n "there";
The choice of quote mark style is left up to you.
Concatenation
You can concatenate strings using the + character:
"Hello " + "There";
It is sometimes useful to compose a string from an array, like so:
["parts", "of", "a", "url"].join("/");
// returns 'parts/of/a/url'
Backtick Strings
Backtick strings allow newlines and embedded code:
const name = "dave"`
hi ${name}
How are you doing today?
`;
Backtick strings are extremely useful for a lot of things.
Strings as arrays
Strings act like arrays. Access a part of a string using the square brace syntax like so:
"Hello there"[0]; // returns "H"
Note that although strings look like arrays, they are not arrays, and don’t give you the full array API.
Handy functions
You can convert a string into an array using the split function:
"Hello".split("");
// ['H', 'e', 'l', 'l', 'o']
You can join the array back up again using the join function:
["H", "e", "l", "l", "o"].join("");
// "Hello"
If you want to reverse a string you can split it, reverse the array, then join them back together again:
"Hello".split("").reverse().join("");
// "olleH"
You can replace a value in a string using the replace function:
"Hello".replaceAll("l", "w").replaceAll("e", "a");
// "Hawwo"
You can also use a regular expression:
"Hello".replace(/l/g, "w");
// "Hewwo"
Exercise - String Concatenation
- Create a variable called greeting. Store the value ‘hello’ in it.
- Create a variable called name. Store the value ‘world’ in it.
- Use your variables to alert “hello world”
- Use a couple of regex to change the output to “heyyy wyryd”