Search code examples
javascriptquotesevaluationdouble-quotes

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.


Solution

  • ' ' 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