Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does ' ', and " ", and no quotes mean in Javascript?

I realized I've been switching between them with no understanding as to why, and am finding it hard to search for.

like image 540
expiredninja Avatar asked Jun 01 '12 14:06

expiredninja


People also ask

What do quotes mean in JavaScript?

Strings in JavaScript are contained within a pair of either single quotation marks '' or double quotation marks "". Both quotes represent 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.

What is difference between single quote and double quotes in JavaScript?

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 \”.

Why do we use backticks in JavaScript?

Backticks ( ` ) are used to define template literals. Template literals are a new feature in ECMAScript 6 to make working with strings easier. Features: we can interpolate any kind of expression in the template literals.

Do numbers need quotes in JavaScript?

Although both are primitive data types but symbols, numbers, boolean, null and undefined are all used without quotes (' '). Whereas, only string is used with quotes. For reference this information is available from from the very initial chapters of JS.


2 Answers

' ' and " " are the same thing; they are used to define string literals.

Things without quotes can be an identifier, keyword, non-string literal, property name or a number (may have missed one).

Examples:

"hello world"        literal (string)
'hello world'        literal (string) with same contents
document             identifier (object)
{ a: 1 }             property name
if                   keyword (start conditional statement)
3.4                  literal (number)
/abc/                literal (regex object)

String literals that are enclosed in single quotes don't need escaped double quotes and visa versa, e.g.:

'<a href="">click me</a>'    HTML containing double quotes
"It's going to rain"         String containing single quote
like image 52
Ja͢ck Avatar answered Sep 28 '22 03:09

Ja͢ck


' ' and " " used to quote string literal and represents string(s) whereas literal without quote are variables (name of variable, constant) know as identifier, example

variable = 'Hello'; (Here `variable` is identifier and 'Hello' is string literal)


var = "Ho There"

You might question, what is the difference between ' (single quote) and " (Double quote)

Difference is ,strings within " if have special character then they need to escape. Example:

Variable = "hi " there"; ---> here you need to escape the " inside string like

Variable = "hi \" there"; 

But if using, ' then no need of escaping (unless there is a extra ' in string). You can hve like

var = 'Hello " World"';
like image 21
Rahul Avatar answered Sep 28 '22 03:09

Rahul