0

For example I could do :console.log(`Hello World`); and it would still run like console.log('Hello World');

The only difference I can see is I can do

let item1 = 'Food';
console.log(`I want ${item1}`);

and it would print I want food, whereas if I go

console.log('I want ${item1}');

using quotation marks it would print "I want ${item1}"

So is the only difference I can see is that I can call variables for one? And if so why do we use quotation marks in console logs?

Name
  • 1
  • 4
  • Use whatever you like. Doesn't make much of a difference in the end. – VLAZ Jul 30 '21 at 07:44
  • 3
    When logging, it's usually better to log the value directly and not stringify it, so `console.log("I want", item1)` will come more handy in most situations – Kaiido Jul 30 '21 at 07:45
  • Yes, that is the only thing. – Kate Jul 30 '21 at 07:48
  • "*I can see is that I can call variables*" you can insert any valid expression but the result might not always be what you expect if the variable you use (or the result of the expression) is not a primitive type. For example, objects would be "stringified" to `[object Object]` and arrays would be joined so that `[1, 2, 3]` becomes `1,2,3`. – secan Jul 30 '21 at 07:55
  • secan, when I did console.log(`1, 2, 3`) it still printed as 1, 2, 3 – Name Jul 30 '21 at 08:00
  • @Botany102 that is not an array. [Try it with an array](https://jsbin.com/vucihas/edit?js,console) – VLAZ Jul 30 '21 at 08:03
  • @secan what is the reason to use an array? – Name Jul 30 '21 at 08:05
  • @secan also thank you for your patience, I'm really new. – Name Jul 30 '21 at 08:06
  • @Botany102, maybe [this image](https://imgur.com/ZQZezNh) will help clarify what I meant. As for why you might use an array, they are a convenient way to group values in an *ordered* collection (if you put something in the first slot, it will always stay there unless you explicitly move it) so they come very handy, for example, when you have to perform the same operation on many different items (e.g. calculate the VAT of each product in you shopping cart). – secan Jul 30 '21 at 08:46
  • @secan thank you, I didn't really understand the image but your explanation makes sense. I would upvote your comment but I don't have enough reputation to. – Name Jul 30 '21 at 09:12
  • @Botany102, the image simply shows that if you try to log an array or an object using a [template literal](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) (a string delimited by back ticks) in the console you will not see the actual value but its stringified version. – secan Jul 30 '21 at 09:18
  • @secan Oh, okay. Thank you. – Name Jul 30 '21 at 10:34

0 Answers0