Open In App

Difference between single-quoted and double-quoted strings in JavaScript

Last Updated : 05 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will see the difference between single-quoted and double-quoted strings in JavaScript.

Both single-quotes and double-quotes strings in JavaScript are used for creating string literals. But the basic difference between them comes into play when the character which needed to be escaped is itself a single-quoted or double-quoted string.

Example: This example shows the usage of single-quoted and double-quoted strings in JavaScript.

Javascript




<script>
    const a = "GeeksforGeeks"
    const b = 'GeeksforGeeks'
    console.log(a,b)
</script>     


Output:

GeeksforGeeks GeeksforGeeks

You need to escape a single quote when the literal is enclosed in a single code using the backslash(\) or need to escape double quotes when the literal is enclosed in a double code using a backslash(\).

Using Single-Quoted strings: While using single-quoted strings for defining string literals, we only need to escape the single quote inside the string. While there is no need to escape double-quote and can be written exactly.

Example: This example shows the single-quoted strings.

Javascript




<script>
    const geeks = 'We will visit \'GeeksforGeeks\' student.';
    const hello = 'Hello, I am an "Engineer"';
    console.log(geeks + "<br>");
    console.log(hello)
</script>    


Output: In this example, GeeksforGeeks has to be written within single quotes. Since, for defining string literal, we are using a single-quote string, that’s why we need to escape the single-quote(‘ ‘) using a backslash(\) within the string. The engineer is quoted in a double-quoted string. Since for defining string literal, we are using a single-quote string, therefore, we do not need to escape the double-quotes(” “) within the string.

We will visit 'GeeksforGeeks' student.
Hello, I am an "Engineer"

Using Double-Quoted strings: While using double-quoted strings for defining string literals, we only need to escape the double-quote inside the string. While there is no need to escape single-quote and can be written exactly.

Example: This example shows the double-quoted strings.

Javascript




<script>
    const geeks = "We will visit \"GeeksforGeeks\" student."
    const hello = "Hello, I am an 'Engineer'"
    console.log(geeks+"<br>")
    console.log(hello)
</script>    


Output: In this example, GeeksforGeeks has to be written within double quotes. Since, for defining string literal, we are using a double-quoted string, that’s why we need to escape the double-quotes(“”) using a backslash(\) within the string. The engineer is quoted in a single-quoted string. Since for defining string literal, we are using a double-quoted string, therefore, we do not need to escape the single-quote(”) within the string.

We will visit "GeeksforGeeks" student.
Hello, I am an 'Engineer'

Whether the string is enclosed in a single quote or in a double quote, they behave in exactly the same manner except for the behavior of escaping characters. That means a string enclosed in the single quote is equal to the same string enclosed in the double quote provided that necessary characters are escaped.

'GeeksforGeeks' === "GeeksforGeeks" 

Example 1: Here, we have declared two constants namely a and b. In both the constants, we have stored the same literal GeeksforGeeks but they both are enclosed differently. the literals in constant a are being enclosed in double-quotes while the literals in constant b are enclosed in single quotes. Then we are comparing constants a and b using a triple equal sign(===) and then write the output of it. Here, a===b will compare the string stored in constants a and b and will return true if they are strictly and exactly equal. If not equal, it will return false. Here the output comes true means that whether the string is enclosed in a single quote or in a double quote, they are strictly and exactly equal with the same value. That simply means Double quotes or Single quotes do not affect the enclosed string. 

Javascript




<script>
    const a = "GeeksforGeeks"
    const b = 'GeeksforGeeks'
    console.log(a===b)
</script>    


Output:

true

Example 2: Using a back-tic or a diacritic sign (`) is the alternative to using a single quote or double quote. In this, you don’t need to escape any single quote or double quote. So, it is an error-free choice. Again the string enclosed in back-tic would be strictly and exactly equal to the strings enclosed in single or double quotes. 

Javascript




<script>
    const geeks = `We will visit "GeeksforGeeks" student.`
    const hello = `Hello, I am an 'Engineer'`
    console.log(geeks+"<br>")
    console.log(hello)
</script>    


Output: Here, as you can see that we have not escaped any double-quote or single while we are enclosing the string in back-tic(`) and this yields us the same output.

We will visit "GeeksforGeeks" student.
Hello, I am an 'Engineer'

Points to remember: While back-tic(`) can be used anywhere and has fewer errors when handling JSON files from within JavaScript, the stringify() and parse() functions have to be enclosed only within double quotes as it knows about the double quotes already.

Out of double quotes and single quotes, which one is preferred: Both quotes can be used anywhere but then, you have to consider the characters which are needed to be escaped.

  • When you have to write double quotes(“) inside the string, preferably choose a single quote string, or even if you are choosing a double-quoted string, don’t forget to escape the double quote inside the string.
  • When you have to write single quotes(‘) inside the string, preferably choose a double-quoted string, or even if you are choosing a single quote string, don’t forget to escape the single quote inside the string.
  • While you can use back-tics(`) in both the above cases and you don’t need to escape anything.

Let us understand the differences in a Tabular Form

single-quoted Strings double-quoted strings
It is a way to specify a string in Javascript It is also a way to specify a string in Javascript
In this way, we generally insert a string in single quotes In this, we generally insert a string in double quotes
The single quotes do not get printed with the string The double quotes do not get printed with the string


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads