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

10 Apr, 2022
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 string. You need to escape single quote when the literal is enclosed in 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: 
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 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: 
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 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 being 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'    document.write(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'`    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 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 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 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 -:
Syntax -:
‘string’
Syntax -:
“string”
Example -:
‘GeeksforGeeks’
Example -:
“GeeksforGeeks”
mayank007rawa
javascript-string
Picked
JavaScript
Web Technologies
Web technologies Questions
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Difference between var, let and const keywords in JavaScript
Difference Between PUT and PATCH Request
How to get character array from string in JavaScript?
Remove elements from a JavaScript Array
How to get selected value in dropdown list using JavaScript ?
Top 10 Front End Developer Skills That You Need in 2022
Installation of Node.js on Linux
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?