Double quotes vs single quotes in JavaScript

38,987

Solution 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 ".

Solution 2

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

Solution 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 :-)

Solution 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.

Share:
38,987
TGH
Author by

TGH

I am a software engineer with focus on web development in particular. Most of my experience comes from using technologies like Angular, JavaScript, C#, HTML and SQL. I am currently focusing on Angular 2.0 Check out my Angular 2.0 articles here: http://www.syntaxsuccess.com/angular-2-articles

Updated on May 02, 2020

Comments