Reputation: 10048
I'm currently playing around with "const" variable in javascript and here is my code
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.
Upvotes: 2
Views: 8740
Reputation: 156
What browser/version are you using? Trying it in both FF65 and Chromium71, console.log( x );
indeed gives me 123 ...
Upvotes: 2
Reputation: 19268
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.
Upvotes: 2