Javascript quotations


#1

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

For this tutorial with console.log we used quotations

console.log("January".substring(0,3));
console.log("Melbourne is great".substring(0,12));
console.log("Hamburgers".substring(3,10));

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

var myCountry = "United States";

console.log(myCountry.length);

console.log(myCountry.substring(0,3));


#2

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.


#3

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


#4

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

console.log(true);

// null

console.log(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;
console.log(a);

// An anonymous function expression

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

// A named anonymous function expression

var foo = function () {};
console.log(foo);
console.log(foo.toString());

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.


#5

Thank you so much! Your response really helped.