Escape quotes in Javascript

Escape quotes in Javascript

In this article, we will see how to escape quotes in Javascript.

Let’s see what happens if we have singe quote in String and we also use single quotes to declare the string in javascript.

var str='Let's learn javascript';
console.log(str);

Output:

unknown: Unexpected token (1:26)

Ways to escape quotes in Javascript

There are multiple ways to escape quotes in Javascript. Let’s go through each of them.

Using escape character

You can use escape character backslash(\) to escape quotes in Javascript. It will prevent javascript to interpret end of String.

You can use \' to escape single quotes and \" to escape double quotes.

Let’s see with the example:
Escape single quotes

var str='Let\'s learn javascript';
console.log(str);

Output:

Let’s learn javascript

Escape double quotes

var str="Let\"s learn javascript";
console.log(str);

Output:

Let”s learn javascript

Using alternate String syntax

You can use opposite string syntax of the one you are currently having.

For example:
If you have single quotes in String, then you can define String in double quotes. Similarly, in case you have double quotes in String, then you can define String in single quotes.
Let’s see with the example:
Escape single quotes

var str="Let's learn javascript";
console.log(str);

Output:

Let’s learn javascript

Escape double quotes

var str='Let"s learn javascript';
console.log(str);

Output:

Let’s learn javascript

Using template literals

Template literals use back ticks(`) rather than quotes to define String. With template literals, you can use both single quotes and double quotes inside a String, so you don;t have to escape quotes in String.

var str=Let's learn javascript and write "Hello world";
console.log(str);

Output:

Let’s learn javascript and write “Hello world”

As you can see, we have used single quotes and double quotes in same String and it just worked fine.

Escape quotes in Javascript in HTML context

If you need to escape quotes in HTML context, then you need to replace quotes with proper XML entity representation & quot;.

Please note that there should not be any space between & quot; here. I had to put a space because while rendering this page, wordpress was changing it to double quotes.

For example:


    
        calculate
    

Let’s say you want to escape quotes "Tax" here in onClick event string.

You need to replace double quotes with " here.

So your onclick string will be:


    
        calculate
    

Please note that there should not be any space between & quot; here. I had to put a space because while rendering this page, wordpress was changing it to double quotes.

That’s all about how to escape quotes in Javascript.

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.