Javascript quotations


Trying to figure the difference of when to use quotations. For example:

For this tutorial with console.log we used quotations

console.log("Melbourne is great".substring(0,12));

But here, after console.log the use of quotations would make these codes invalid. How come?

var myCountry = "United States";




Quotation marks are only ever used for string literals, or plain text. Identifiers (variables) are not strings in the literal sense, though they may refer to a string.


so when ever console.log is referring to a var it's typical to not use quotations?


console.log() takes any object and conveys it to the display in plain text form. The mechanics are not important, just now, so let's look at some examples.

// A string literal

console.log("This is a plain text string");

// A number

console.log(6 * 7);

// A boolean


// null


// An array

console.log([1, 2, 3, 5, 7, 9, 11, 13]);

// A plain object

console.log({question: "What is the meaning of life?", answer: 42});

// undefined

var a;

// An anonymous function expression

console.log(function () {});
console.log((function () {}).toString());

// A named anonymous function expression

var foo = function () {};

In the last example we introduce a variable, foo. This should give you some insight into how log addresses them.

Correct. We only ever use quotes when passing a string primitive for logging.

Quotes are also useful in denoting attribute values but that is another thing, yet to be explored. For our purposes, at this point, think of anything in quotes, single or double, as a string of plain text. It is not code, but something to be used by or analyzed by the code.


Thank you so much! Your response really helped.