The most likely reason for use of single vs. double quote in different libraries is programmer preference and/or API consistency. Other than being consistent, use whichever best suits the string.

Using the other type of quote as a literal:

alert('Say "Hello"');
alert("Say 'Hello'");

This can get complicated:

alert("It's \"game\" time.");
alert('It\'s "game" time.');

Another option, new in ECMAScript 6, is template literals which use the backtick character:

alert(`Use "double" and 'single' quotes in the same string`);
alert(`Escape the \` back-tick character and the \${ dollar-brace sequence in a string`);

Template literals offer a clean syntax for variable interpolation, multi-line strings, and more.

Note that JSON is formally specified to use double quotes, which may be worth considering depending on system requirements.

🌐
W3docs
w3docs.com › javascript
When to Use Double or Single Quotes in JavaScript
But there is no need to escape the other character inside a string. Hence, a double quote can have single quotes without escaping them, and vice versa. An important argument for single quotes is when you need to write html inside JavaScript:
Discussions

Do you use Single Quotes or Double Quotes?
Missing third option: whatever my formatter is set up to use. (Which depends on what the team decides at the start of the project) More on reddit.com
🌐 r/learnjavascript
68
23
June 10, 2022
Difference between single quotes and double quotes in Javascript - Stack Overflow
I know that in PHP, the only difference between double quotes and single quotes is the interpretation of variable inside a string and the treatment of escape characters. In JavaScript, I often see More on stackoverflow.com
🌐 stackoverflow.com
syntax - Double quotes vs single quotes in JavaScript - Stack Overflow
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 q... More on stackoverflow.com
🌐 stackoverflow.com
html - How do I escape a single quote ( ' ) in JavaScript? - Stack Overflow
UPDATE: I want to give an updated answer to this question. First, let me state if you're attempting to accomplish what I have below, I recommend that you manage events by adding event listeners ins... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Flexiple
flexiple.com › javascript › double-vs-single-quotes-javascript
'Single' vs "Double" quotes for strings in javascript - Flexiple
March 10, 2022 - 'apple' //correct "apple" //correct "apple' //incorrect · The system doesn't care which one you use. On German, Hungarian, Austrian, and many other keyboards, you have to use the Shift key for both single or double-quotes.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › when-should-we-use-double-or-single-quotes-in-javascript
When should we use double or single quotes in JavaScript ? - GeeksforGeeks
July 23, 2025 - JSON (JavaScript Object Notation) only supports double quotes rather than single quotes for copying and pasting files.
🌐
javascript.com
javascript.com › learn › strings
JavaScript Strings: The Basic Methods and Functions | JavaScript.com
Strings in JavaScript are contained ... Strings but be sure to choose one and STICK WITH IT. If you start with a single quote, you need to end with a single quote....
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › difference-between-single-quoted-and-double-quoted-strings-in-javascript
Difference Between Single-Quoted And Double-Quoted Strings in JavaScript - GeeksforGeeks
July 15, 2025 - Escape Characters: To include a double quote within a double-quoted string, use a backslash (\). ... Consistency is Key: While there is no performance difference between the two, it’s important to stick to a consistent style throughout your ...
🌐
DEV Community
dev.to › ziizium › understanding-and-using-quotes-in-javascript-1n7
Understanding and Using Quotes in JavaScript - DEV Community
November 11, 2024 - Meanwhile, the quote contains a string with double quotes, so we use single quotes to enclose the entire string to prevent a syntax error. Nested quotes, if not handled properly, can lead to syntax errors (more on this later).
Top answer
1 of 5
204

You should always consider what the browser will see by the end. In this case, it will see this:

<img src='something' onmouseover='change(' ex1')' />

In other words, the "onmouseover" attribute is just change(, and there's another "attribute" called ex1')' with no value.

The truth is, HTML does not use \ for an escape character. But it does recognise &quot; and &apos; as escaped quote and apostrophe, respectively.

Armed with this knowledge, use this:

document.getElementById("something").innerHTML = "<img src='something' onmouseover='change(&quot;ex1&quot;)' />";

... That being said, you could just use JavaScript quotes:

document.getElementById("something").innerHTML = "<img src='something' onmouseover='change(\"ex1\")' />";
2 of 5
187

The answer here is very simple:

You're already containing it in double quotes, so there's no need to escape it with \.

If you want to escape single quotes in a single quote string:

var string = 'this isn\'t a double quoted string';
var string = "this isn\"t a single quoted string";
//           ^         ^ same types, hence we need to escape it with a backslash

or if you want to escape \', you can escape the bashslash to \\ and the quote to \' like so:

var string = 'this isn\\\'t a double quoted string';
//                    vvvv
//                     \ ' (the escaped characters)

However, if you contain the string with a different quote type, you don't need to escape:

var string = 'this isn"t a double quoted string';
var string = "this isn't a single quoted string";
//           ^        ^ different types, hence we don't need escaping
🌐
Javascript-exam
javascript-exam.com › home › blog › javascript strings › you can use both single and double quotes to define a string in javascript
You Can Use Both Single and Double Quotes to Define a String in JavaScript
February 20, 2026 - You can escape the quotes using a backslash, or consider using template literals to handle more complex strings. No, there are no performance differences between the two in JavaScript.
Top answer
1 of 3
2
Hey Monique! Could you share what error you're getting from using double quotes with "sun". Just curious as there shouldn't be any error as far as I know. As for the "console.log" statements needing to use double quotes for their string literals. It all comes down to the fact that the strings contain apostrophes in words like "I'm", and "It's". Due to the fact that the apostrophe and the single quote share the same key, you must use double quotes otherwise the javascript engine will believe that the string ends at the apostrophe. This will likely causing errors as it tries to figure out what everything else means. What happens when using 'single quotes': javascript console.log('I do not know if I'm going to the supermarket today') javascript // In the example above, the javascript engine only recognizes: // 'I do not know if I' as the entire string. (Highlighted in orange) Additionally, since the string is concluded at the single quote in the word "I'm", the single quote at the end, after the word today, marks the beginning of a new string literal that doesn't have an end. This is something that will likely cause javascript to throw an error. What happens when using "double quotes": ```javascript console.log("I do not know if I'm going to the supermarket today.") // In this example above, the javascript engine recognizes: // "I do not know if I'm going to the supermarket today" as the entire string (Highlighted in orange) ``` Same thing can happen when trying to use multiple double quotes ```javascript console.log("I would like to "Jimmy" to step up to the plate.") // In the example above, the javascript engine recognizes: // "I would like to " and " to step up to the plate." as two separate strings. (Highlighted in orange) ``` In this example the word "Jimmy" isn't included in the string and will likely throw up an error as it's unexpected in the syntax. What happens when using 'single quotes' ```javascript console.log('I would like "Jimmy" to step up to the plate') // In this example above, the javascript engine recognizes: // 'I would like "Jimmy" to step up to the plate' as the entire string (Highlighted in orange) ``` Doing it this way would yield the intended result. Using an escape character If you decided you really want to use all single quote for a string that contains apostrophes, or double quotes within a string literal created with double quotes. You do have the option of using an escape character before the the respective quote mark. ```javascript console.log('I do not know if I\'m going to the supermarket today.') // javascript will recognize this as: // 'I do not know if I'm going to the supermarket today' not displaying showing the backslash. console.log("I would like to \"Jimmy\" to step up to the plate.") // javascript will recognize this as: // "I would like to "Jimmy" to step up to the plate." not displaying showing the backslashes. ``` Adding the backslash () allows the ' or the " to be used within the string. However, without ending the string literal. For more information check out this great article on strings from w3schools! (https://www.w3schools.com/js/js_strings.asp) Hopefully this answers your question!
2 of 3
0
I wish there were like a tree or a list of all the things Ive watched before that I could easily go back and forth with bc some things i retain and others i totally dont ha.
🌐
Medium
medium.com › geekculture › string-literals-in-javascript-should-i-use-double-quotes-or-single-quotes-3639342480f9
String Literals in JavaScript: Should I Use Double-quotes or Single-quotes? | by Bernhard Häussermann | Geek Culture | Medium
May 12, 2021 - Now, regardless of whether you are decided on using single quotes or double quotes in JavaScript, the more important issue is applying your convention consistently throughout the code base. ESLint is one tool that will help a great deal in enforcing a consistent coding style. You supply it with a config file specifying a list of rules describing your coding conventions, and it will indicate where your code is not conforming to the specified rules.
🌐
Bytearcher
bytearcher.com › articles › single-or-double-quotes-strings-javascript
Should I use 'single' or "double-quotes" for strings in JavaScript
There is no type for a single character in JavaScript - everything is always a string. ... Strategic selection of the quote character can save you from escaping ' or " characters inside the string.
🌐
Staxmanade
staxmanade.com › 2018 › 03 › should-i-use-javascript-single-or-double-quotes
Should I Use JavaScript Single (') or Double (") Quotes? - Developing on Staxmanade
March 15, 2018 - On German, Hungarian, Austrian, and many other keyboards, you have to use the Shift key for both single or double quotes. On Turkish Q keyboards it's apparently the other way around (need <Shift> for single quote vs double). One popular argument for single quotes is when you have to write html within javascript:
🌐
SitePoint
sitepoint.com › javascript
Double and single quotes in JS inside HTML attributes - JavaScript - SitePoint Forums | Web Development & Design Community
July 2, 2010 - I know that using and is evil, but in this particular situation it is quite hard to avoid it due to external powers 😛 Anyway here is the issue itself : putting javascript strings inside HTML attributes : Everything is fine until there are ...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-escape-all-single-and-double-quotes-in-javascript
How to Escape All Single and Double Quotes in JavaScript? - GeeksforGeeks
November 8, 2024 - Regular Expression: The replace() method uses the regex /['"]/g to find all occurrences of single (') and double (") quotes. The g flag ensures all matches are replaced. ... \\$&: Escapes the matched character by prefixing it with a backslash ...
🌐
javaspring
javaspring.net › blog › do-and-have-different-meanings-in-javascript
Do Double Quotes ("") and Single Quotes ('') Have Different Meanings in JavaScript? When to Use Each Explained — javaspring.net
If your string includes single quotes (e.g., contractions like “don’t” or possessives like “Alice’s”), using double quotes for the outer string avoids the need to escape the inner single quotes.