简体   繁体   中英

What is the difference between ' and " in JavaScript?

<button onClick="function("parameter")">Click</button>
<button onClick="function('parameter')">Click</button>

var variable = "value";
var variable = 'value';

Is there a difference?

No. They are interchangeable by design.

The only requirement is that you need matching pairs (either use " or ' , but not both to signify a string).

See the spec for string literals:

StringLiteral:
          " StringCharactersDQopt " 
          ' StringCharactersSQopt '

When used within HTML, you need to be careful not to use the same delimiter in HTML attributes as the javascript ones (which is why your first example is not legal).

To function correctly you would need to change it to:

<button onClick='function("parameter")'>Click</button>

Yes, there's a difference when you mix it with HTML like you do: the first snippet will throw an exception because you need to escape the double quotes while the second will work (that's one of the reasons why you should avoid mixing markup with javascript). In pure javascript (a separate file) there's no difference.

The two are equivalent as long as the same one is used at both the beginning and end of the string literal. That said, choosing the correct one can avoid needless string escaping:

<button onClick="function(&quot;parameter&quot;)">Click</button> <!-- becomes -->
<button onClick="function('parameter')">Click</button>

var foo = "And the computer said: \"Hello, world!\""; // becomes
var foo = 'And the computer said: "Hello, world!"';

This has a clear advantage when using JavaScript to generate HTML, as is common in scripts using jQuery.

There is no difference. ' and " are interchangeable. Now you can't have a string like this: var my_var = 'hello world" ; Opening and close quotes have to match. This does allow you to easily do: var my_variable = 'John says "I love JavaScript."' without having to escaping anything.

so this: <button onClick="function("parameter")">Click</button> won't work because you have opened and closed the onclick event prematurely "function("

It is the same. The only reason for having ' and " is, that you cannot nest them, for example, in given onClick example ->

onClick=" and there you need to use ' to encapsulate string "

The only time that there is a difference (that I am aware of) is in JSON: Double quotes are required around the keys and values.

And yes, everyone is right; Your first line should read like this or it will throw an error:

<button onClick='function("parameter")'>Click</button>

I prefer to use the double quotes only (if possible) because I'm used to C like languages (C, C++, C#) where string can be wrapped only with double quotes. (Single quotes is used, but to wrap char type)

To answer the question itself: same like all others said, no real difference - guess it exists to support cases when you mix JavaScript with HTML.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM