Home >Web Front-end >JS Tutorial >What is the difference between single quotes and double quotes in javascript

What is the difference between single quotes and double quotes in javascript

青灯夜游
青灯夜游Original
2021-09-07 12:13:187826browse

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 "" \"\""", "'\'\ ''".

What is the difference between single quotes and double quotes in javascript

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+&#39;<br>&#39;);
docunment.write(str2+&#39;<br>&#39;);

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 = &#39;I love "JavaScript"&#39;;
var str2 = "I love &#39;JavaScript&#39;";
document.write(str1);
document.write(str2);

The result is as follows:

I love "JavaScript"
I love &#39;JavaScript&#39;

Example: Use all single quotes or all double quotes

var str1 = &#39;I love &#39;JavaScript&#39;&#39;;
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 = &#39;I love \&#39;JavaScript\&#39;&#39;;
var str2 = "I love \"JavaScript\"";
document.write(str1);
document.write(str2);

The result is as follows:

I love &#39;JavaScript&#39;
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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn