Skip to content
Related Articles

Related Articles

Improve Article

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

  • Last Updated : 22 Jun, 2020
Geek Week

Both single-quoted and double-quoted 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 strings. You need to escape single quote when the literal is enclosed in single code using backslash(\) or need to escape double quotes when the literal is enclosed in double code using 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: 

Javascript




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

Output: In this example, GeeksforGeeks has to be written within single quotes. Since, for defining string literal, we are using 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 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: 

Javascript




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

Output: In this example, GeeksforGeeks has to be written within double-quotes. Since, for defining string literal, we are using double-quote 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 double-quote 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 is being enclosed in single quotes. Then we are comparing constants a and b using triple equal sign(===) and then writing 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 does not affect the enclosed string.

Javascript




<script>
    const a = "GeeksforGeeks"
    const b = 'GeeksforGeeks'
    document.write(a===b)
</script>    

Output: 

true
  • Example 2: Using a back-tic or a diacritic sign (`) is the alternative of using 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'`
    document.write(geeks+"<br>")
    document.write(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 have less error, but when handling JSON files from within JavaScript, the stringify() and parse() functions has to be enclosed only within double quotes as it know about the double quotes already. 

Out of double quote and single quote, 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 single quote string or even if you are choosing double-quote string, don’t forget to escape the double quote inside the string.
  • When you have to write single quotes(‘) inside the string, preferably choose double-quote string or even if you are choosing 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.

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it’s lowest price ever!




My Personal Notes arrow_drop_up
Recommended Articles
Page :