
Join the Conversation!
Subscribing gives you access to the comments so you can share your ideas, ask questions, and connect with others.
In this lesson, you'll learn about strings in JavaScript, which are used to store and manipulate text.
In JavaScript, and in any programming language for that matter, we need a way to store text. In JavaScript, we use strings to store text. A string is a primitive data type that represents a sequence of characters.
There are a few ways to create strings in JavaScript:
const single = 'This is a string written inside of single quotes.';
const double = "This is a string written inside of double quotes.";
const backticks = `This is a string written inside of backticks.`;
Backticks allow for string interpolation, where you can embed expressions inside a string:
const backticks = `${2 + 2}`; // 4
You can also make function calls inside a string:
const sum = (a, b) => a + b;
const total = `The sum is ${sum(2, 2)}`; // The sum is 4
Backtick strings can span multiple lines:
const numbers = `
1
2
3
`;
If you tried doing this with basic single or double quote strings, you would get an error.
Consider the following string:
const greeting = 'Hi, I'm John';
This would produce an error because the single quote after "I" ends the string prematurely. JavaScript doesn't know how to evaluate the rest of the code.
One way to fix this is to use different types of quotes: , ,
const greeting = "Hi, I'm John.";
But if you have both types of quotes in the sentence, it can get tricky:
const greeting = "Hi, I'm John, "Johnny John".";
This would break again. To handle this, you can use an escape character to treat special characters like normal letters:
const greeting = 'Hi, I\\'m John, \\"Johnny John\\".';
However, using backticks simplifies this:
const greeting = `Hi, I'm John, "Johnny John".`;
This way, you can write the string however you want without worrying about escaping quotes .
Great! This lesson covered all the basics of strings, and you've learned it all! Strings are pretty simple, yet powerful tools in JavaScript.
"Please login to view comments"
Subscribing gives you access to the comments so you can share your ideas, ask questions, and connect with others.
How did you manage to remove the blur property and reach here?
Upgrading gives you access to quizzes so you can test your knowledge, track progress, and improve your skills.