In this lesson you will learn all about strings. This is a long lesson so take your time and experiment with the examples.
What Are Strings
Strings are the data type that hold text content. They can be any series of characters, from an empty string to a long paragraph of text and any length of characters in between. Strings are denoted in JavaScript with either single quotes, double quotes, or backticks.
// Example String Formats:
let stringWithQuotes = "Hear ye, hear ye";
let stringWithDoubleQuotes = "I am Julius Ceasar";
let stringWithBackticks = `Veni vidi vici`;
There is no functional difference between single quotes and double quotes, but backticks (`) have some functional differences you will see below.
How To Manipulate Strings
When working with strings, two important manipulations that you should master are concatenation and interpolation.
Concatenation
String concatenation is the action of merging two or more strings into one. You can do so by adding the strings together. Have a look at the example below:
let greeting = "Hello " + "Nomads";
console.log(greeting); // "Hello Nomads"
const day = "Monday";
const person = "Stephanie";
greeting = "Hello " + person + ", Happy " + day;
console.log(greeting); // "Hello Stephanie, Happy Monday"
Interpolation
String interpolation is denoted by using backticks. Inside any string surrounded by backticks, you can insert JavaScript to be interpolated into it. This is done by inserting the variable or expression with a preceding $ and containing it in curly braces.
const day = "Monday";
const person = "Stephanie";
let greeting = `Hello ${person}, Happy ${day}`;
console.log(greeting); // "Hello Stephanie, Happy Monday"
String interpolation isn't reserved for only variables, you can insert anything to be evaluated and placed into the string.
let minutesPerHour = 60;
let hoursPerDay = 24;
let message = `There are ${minutesPerHour} minutes per hour and ${hoursPerDay} hours per day, which makes ${minutesPerHour * hoursPerDay} minutes in a day`;
console.log(message); // "There are 60 minutes per hour and 24 hours per day, which makes 1440 minutes in a day"
When to Use Single Quotes, Double Quotes or Backticks
As mentioned at the beginning of the lesson, Javascript recognizes strings by the presence of either single quotes, double quotes or backticks. Choosing between them depends on the desired functionality.
Single and double quotes are often interchangeable but backticks are necessary when you want to apply string interpolation. Strings wrapped in backticks will also pay attention to spacing and line breaks, whereas strings wrapped in single or double quotes will ignore more than a single space and line breaks unless special character are added.
// These two strings are the same:
let str1 = "Hello \nNomads";
let str2 = `Hello
Nomads`;
Some Special Characters
Some other special characters that can be added to strings are:
The backslash \ is used to escape characters. So if you would like a string with single quotes to display another single quote, you would use the backslash to escape the ' so it can be included in the string:
console.log('I\'m a Nomad!'); // "I'm a Nomad!"
You can also add single quotes or tes to a string by using the opposite to wrap the string
console.log("I'm a Nomad!"); // "I'm a Nomad!"
Summary: Javascript Strings
- Strings are a data type that hold text content
- In Javascript they are denoted by single quotes, double quotes or backticks
How to Manipulate Strings
- Concatenation is the merging of two or more strings into one
- Interpolation is the insertion of some Javascript inside of a string
When to Use Single Quotes, Double Quotes or Backticks
- Single quotes, double quotes and backticks can all be used to denote a string in Javascript
- Their usage depends on the desired functionality
- Backticks are the only ones that allow for string interpolation
Some Special Characters
- You can manipulate strings by adding some special characters