2

I'm currently playing around with "const" variable in javascript and here is my code

enter image description here

My question is, why is "const x" undefined when used with "console.log", but it is defined when used on its own?

p.s. -- I understand that both global "const, let" do not become the property of "window" object, unlike global "var". But I am unsure as to whether this played any role in the code above.

Thor
  • 9,638
  • 15
  • 62
  • 137
  • Possible duplicate of [Chrome/Firefox console.log always appends a line saying undefined](https://stackoverflow.com/questions/14633968/chrome-firefox-console-log-always-appends-a-line-saying-undefined) – Randy Casburn Jan 18 '19 at 02:35

2 Answers2

2

You're seeing undefined because console.log() function actually returns you that.

Notice how x is still 123 when you query just x?

The assignment operation for const x = 123; is undefined because it returns you undef as well.

Samuel Toh
  • 18,006
  • 3
  • 24
  • 39
  • can you please explain to me why "console.log(x);" does not print out the value of "const x"? – Thor Jan 18 '19 at 02:42
  • 2
    The value should be printed in the `console`, not in your debug window. The latter is meant for you to `debug` what value gets returned from a particular operation not what is printed. – Samuel Toh Jan 18 '19 at 02:44
1

What browser/version are you using? Trying it in both FF65 and Chromium71, console.log( x ); indeed gives me 123 ...

do or do not
  • 146
  • 1
  • 5