Home >Web Front-end >JS Tutorial >What is the difference between single quotes and double quotes in javascript
In JavaScript, there is no difference between single quotes and double quotes, but they must be used uniformly. If quotation marks are used again, they can be in the format of "double outside and single inside" or "single outside and double inside"; or use backslashes to escape, such as "" \"\""", "'\'\ ''".
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
There is actually no difference between single quotes and double quotes in JavaScript. It depends on how you get used to it.
/*先分别用单引号、双引号初始化字符串*/ var str1='I love JavaScript'; var str1="I love JavaScript"; docunment.write(str1+'<br>'); docunment.write(str2+'<br>');
The result is as follows:
I love JavaScript I love JavaScript
But if quotation marks are used again, we can adopt the format of "double outside and single inside" or "single outside and double inside"; if double quotes are needed itself, it can only be "single outside and double inside":
Example: Initialize the string using a mixture of single and double quotes
var str1 = 'I love "JavaScript"'; var str2 = "I love 'JavaScript'"; document.write(str1); document.write(str2);
The result is as follows:
I love "JavaScript" I love 'JavaScript'
Example: Use all single quotes or all double quotes
var str1 = 'I love 'JavaScript''; var str2 = "I love "JavaScript""; document.write(str1); document.write(str2);
The result is an error in the program!
Example: Transfer with backslash
var str1 = 'I love \'JavaScript\''; var str2 = "I love \"JavaScript\""; document.write(str1); document.write(str2);
The result is as follows:
I love 'JavaScript' I love "JavaScript"
So the fool thinks the conclusion is as follows
Conclusion:
(1) There is no difference between single quotes and double quotes for strings that only use characters
(2) Double quotes can be used directly in strings enclosed by single quotes, and in strings enclosed by double quotes You can use single quotes directly
(3) If you use double quotes in a string enclosed by double quotes, you need to escape them with a backslash, note "\"; also in a string enclosed by single quotes When using single quotes, you also need to escape "\"
[Recommended learning: javascript advanced tutorial]
The above is the detailed content of What is the difference between single quotes and double quotes in javascript. For more information, please follow other related articles on the PHP Chinese website!