JavaScript

How to Escape a Single Quote From a String in JavaScript

A string includes a sequence of characters, numbers, letters, or symbols. It also contains some special characters like comma(,), forward-slash(/), backward-slash(\), single quotes(‘’), double quotes(“”) etc. In JavaScript, if a string contains one or more whitespaces, it must be enclosed in double or single quotes. However, if the single quotes are present inside the string, it generates a syntax error.

This guide will explain the complete procedure to escape a single quote(‘) from a string in JavaScript.

How to Escape a Single Quote From a String in JavaScript?

The “\(backslash)” is an escape character that escapes the special characters from the initialized string. It tells the interpreter to treat the next specified character as a “literal” character instead of a special character. The “literal” characters are the fixed values that represent the string values in the source code.

Let’s use the “\ (backslash)” escape character practically to escape a single quote from the initialized string.

HTML Code

First, consider the following HTML code:

<h2>Escape a single quote ( ' ) From a String</h2>
<p>In JavaScript, you can escape a single quote by using the \ (backslash) Escape character.</p>

In the above lines of code:

  • The “<h2>” tag defines the stated subheading of the second level.
  • Next, the “<p>” tag creates a paragraph specifying the given statement.

JavaScript Code (Without Using “\ (Backslash)” Escape Character)

Follow the below-stated code:

<script>

let string = 'The string contains 'single quote'.';
console.log(string);

</script>

In this code block:

  • Declare a variable “string” of data type “let” that specifies the defined string. It also has a single quote inside the string.
  • Now, apply the “log()” method to print the “string” value as an output.

Output

The “single quote(‘)” inside the string does not work properly and returns an error as indicated in the below output:

The console represents the presence of a single quote within a string as a “syntax” error.

JavaScript Code (Using “\ (Backslash)” Escape Character)

Consider the given code snippet:

<script>

let string = 'The string contains \'single quote\'.';
console.log(string);

</script>

Here, the initialized string uses the “\(backslash)” escape character alongside the single quotes to escape them and return the string without any faced limitations. 

Output

Here, it can be implied that the usage of the “\(backslash)” escape character escaped the single quote and displayed the resultant output.

Conclusion

JavaScript provides the “\(backslash)” escape character to escape the single quote (‘) from a string. It handles the special characters as a string literal rather than the string delimiter or special character. Apart from single quotes, it can be implemented for escaping all special characters from a string. This guide demonstrated all possible aspects to escape a single quote from a string.

About the author

Talha Saif Malik

Talha is a contributor at Linux Hint with a vision to bring value and do useful things for the world. He loves to read, write and speak about Linux, Data, Computers and Technology.