What does single quote mean in javascript?

I'd like to say the difference is purely stylistic, but I'm really having my doubts. Consider the following example:

/*
    Add trim() functionality to JavaScript...
      1. By extending the String prototype
      2. By creating a 'stand-alone' function
    This is just to demonstrate results are the same in both cases.
*/

// Extend the String prototype with a trim() method
String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, '');
};

// 'Stand-alone' trim() function
function trim(str) {
    return str.replace(/^\s+|\s+$/g, '');
};

document.writeln(String.prototype.trim);
document.writeln(trim);

In Safari, Chrome, Opera, and Internet Explorer (tested in Internet Explorer 7 and Internet Explorer 8), this will return the following:

function () {
    return this.replace(/^\s+|\s+$/g, '');
}
function trim(str) {
    return str.replace(/^\s+|\s+$/g, '');
}

However, Firefox will yield a slightly different result:

function () {
    return this.replace(/^\s+|\s+$/g, "");
}
function trim(str) {
    return str.replace(/^\s+|\s+$/g, "");
}

The single quotes have been replaced by double quotes. (Also note how the indenting space was replaced by four spaces.) This gives the impression that at least one browser parses JavaScript internally as if everything was written using double quotes. One might think, it takes Firefox less time to parse JavaScript if everything is already written according to this 'standard'.

Which, by the way, makes me a very sad panda, since I think single quotes look much nicer in code. Plus, in other programming languages, they're usually faster to use than double quotes, so it would only make sense if the same applied to JavaScript.

Conclusion: I think we need to do more research on this.

This might explain Peter-Paul Koch's test results from back in 2003.

It seems that single quotes are sometimes faster in Explorer Windows (roughly 1/3 of my tests did show a faster response time), but if Mozilla shows a difference at all, it handles double quotes slightly faster. I found no difference at all in Opera.

2014: Modern versions of Firefox/Spidermonkey don’t do this anymore.

In JavaScript, single (‘ ’) and double (“ ”) quotes are frequently used for creating a string literal.

Generally, there is no difference between using double or single quotes, as both of them represent a string in the end.

There is only one difference in the usage of single and double quotes, and it comes down to what quote character you need to escape using the backslash character (\): \’ or \”. Each type of quote should escape its own type.

For instance:

"double quotes ( \" ) should escape a double quote"
'single quotes ( \' ) should escape a single quote'

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:

  • When you use single quotes you can act as follows:

    What does single quote mean in javascript?
    Javascript single quotes

    let html = '

    '; alert(html);

  • But if you use double quotes, you need to escape every " EX :

    What does single quote mean in javascript?
    Javascript double quotes

    let html = "

    "; alert(html);

    which can be pretty annoying.

  • Single quotes look better when you use them for representing an empty string '' vs "" .

The only disadvantage of single quotes that you may come up with is copying and pasting between JSON and JavaScript files: single quotes are not supported within JSON files.

On the contrary, JSON allows using double quotes only. Also, using double quotes, you get rid of apostrophes while writing English letters.

There is also an alternative and efficient solution: using backticks (` `) Using this type of quote has many advantages.

First and foremost, they allow simple string concentration (“variable interpolation”). Here is an example:

"string " + variable becomes `string ${variable}`

Secondly, there is no need to escape (\) single or double quotes. Take a look at this example:

"\"Welcome to W3Docs!\""
becomes `"Welcome to W3Docs"`

Thirdly, they allow multi-line code without new line character (\n):

"Welcome to\nW3Docs!"
becomes 
`Welcome to W3Docs`

Also, they operate better for HTML; hence you can use them by default, as follows:

What does single quote mean in javascript?
Javascript multi line code for HTML

const value = `

Loading...

`; alert(value);

To avoid making a choice every time you intend to write a string, we recommend you pick one of the styles described above and stick with it. Single quotes are the most common ones in contemporary programming. But, always keep in mind that JSON does not support them.

The strings in JavaScript are generally used to store and manipulate text. UTF-16 is their persistent internal format. Strings are capable of representing zero or more characters, written inside quotes.

Тhere are three ways to write strings: you can write them inside single quotes, double quotes, or backticks. The quote type that is used should match on both sides. Anyway, it is possible to use all of the quotes within the same script. Strings that use single quotes and double quotes are considered effectively the same. The final and newest way of using the strings is called a template literal: it uses backticks and works the same ways as regular strings.

What is single quote in JavaScript?

In JavaScript, single quotes ( '' ) and double quotes ( “” ) are used to create string literals. Most developers use single or double quotes as they prefer, and sometimes they let their code formatters decide what to use.

How do I put single quotes in JavaScript?

Strings are created by putting data inside the quotes. JavaScript and other programming languages allow the users to keep the data either in double quotes (" ") or single quotes (' ').

What does a single quote mean?

Quote direct speech in single quote marks. Single quotation marks are also known as 'quote marks', 'quotes', 'speech marks' or 'inverted commas'. Use them to: show direct speech and the quoted work of other writers. enclose the title of certain works.

What is the use of single quote in Java?

A string literal is bracketed by either single quotes ( ' ) or double quotes ( " ). When single quotes bracket a string literal, the value of the literal is the value within the quotes. When double quotes are used, any references to variables or expressions within the quotes are interpolated.