I realized I've been switching between them with no understanding as to why, and am finding it hard to search for.
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.
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 \”.
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.
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.
' '
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
' '
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"';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With