Double quotes vs single quotes in JavaScript [duplicate]

假装没事ソ 提交于 2019-11-29 04:46:37

问题


Possible Duplicate:
When to Use Double or Single Quotes in JavaScript
Are there differences between ' and "

I am wondering if there is a difference between using single quotes vs double quotes in JavaScript (JQuery selectors etc.).

Both seem to work just fine, so is there a difference?


回答1:


The difference is that you don't need to escape single quotes in double quotes, or double quotes in single quotes. That is the only difference, if you do not count the fact that you must hold the Shift key to type ".




回答2:


It doesn't matter unless you're trying to write JSON, in which case you must use double quotes for object literals.




回答3:


There is no special difference between single and double quotes (like it is in PHP).

The only actual difference is that you can write a double quote " in a single-quoted string without escaping it, and vice versa.

So, if you are going to output some HTML in your JavaScript, like this:

<div id = "my_div"></div>

single quotes are more useful :-)




回答4:


They work the same, but they must match. You cannot start a string with a single and end with a double, or the opposite. Other than that, they are interchangeable.



来源:https://stackoverflow.com/questions/9959928/double-quotes-vs-single-quotes-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!