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

I realized that I was switching between them, not understanding why, and it’s hard for me to find.

+7
source share
4 answers

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

Things without quotes can be an identifier, a keyword, not a string literal, a property name, or a number (perhaps one was missed).

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 enclosed in single quotes do not need double quotes and vice versa, for example:

 '<a href="">click me</a>' HTML containing double quotes "It going to rain" String containing single quote 
+16
source

' ' and " " used to quote a string literal and represents string (s), whereas a literal without a quote is known as variables (variable name, constant) as an identifier, example

 variable = 'Hello'; (Here `variable` is identifier and 'Hello' is string literal) var = "Ho There" 

You may ask, what is the difference between ' (single quote) and " (Double quote)

The difference is that the lines inside " , if they have a special character, then they need to escape. Example:

Variable = "hi" there "; ---> here you need to avoid the inner line " , for example

 Variable = "hi \" there"; 

But if you use ' , then there is no need to escape (if there is no unnecessary ' in the string). you can use

 var = 'Hello " World"'; 
+6
source

" and ' are interchangeable (but must be used together).

myObject["property"] and myObject.property also interchangeable. $var foo = "property"; myObject[foo] $var foo = "property"; myObject[foo] (in the comments below).

+5
source

Fast jsfiddle around and both single and double quotes control codes etc.

In recent days, I had errors from HTML where double quotes were not used, and if you look at the spec for JSON you will notice that this is a double quote that is requested when quoting string literals. So these are double quotes, which, in my opinion, are convention for historical reasons.

However! These days of writing the back end of JS, I have to admit that I'm leaning towards my C roots and double quote where I need escaped characters and single quote strings that are actually literal characters and will never contain escaped characters (although this is essentially unproductive behavior). Also, in any case, most of my JS - coffeescript is currently, no one has ever written javascript for elegance, CS is a different fish maker, though.

0
source

Source: https://habr.com/ru/post/917193/


All Articles