JavaScript strings and quotes

JavaScript strings and quotes

We already know that we have a string data type, lets understand a little more about strings.

You create a string by using a single or double quotes. 

let name = "Doers";

We stored the word Doers in name. I want you to create a variable to store your name and display it.

If you've completed the previous lesson you know how to use the console.log statement. Otherwise revisit and look at the article

Let's consider a scenario where you want to display a quotation mark within the string. 

var quoteByLisa = "Lisa says "Life is beautiful"";

You can't directly do something like this in JavaScript. You will have to escape quotes in string. Stay with me, I'll show you exactly how.

In JavaScript, you can escape a quote using backslash (\). You will have to use it two times.

var quoteByLisa = "Lisa says \"Life is beautiful\"";

Note the first is before the quote and the other is at the end. Try it out. 

If you use single quotes, you could not have to escape. But there are certain situations where this will not work. If you are doing something simple, doing this is fine.

var quoteBySantosh = 'Santosh says "Life is lovely, and charming".';

If you log this to console this would show correctly as Santosh says "Life is lovely, and charming".

More on this later. For now you are fine. For now, I would suggest you use single quotes while using HTML tags. It will make your life easier.

Post a Comment