最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - "const" variable not defined when used with "console.log" - Stack Overflow

programmeradmin4浏览0评论

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 bee the property of "window" object, unlike global "var". But I am unsure as to whether this played any role in the code above.

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 bee the property of "window" object, unlike global "var". But I am unsure as to whether this played any role in the code above.

Share Improve this question asked Jan 18, 2019 at 2:32 ThorThor 10.1k15 gold badges73 silver badges144 bronze badges 1
  • Possible duplicate of Chrome/Firefox console.log always appends a line saying undefined – Randy Casburn Commented Jan 18, 2019 at 2:35
Add a ment  | 

2 Answers 2

Reset to default 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.

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

发布评论

评论列表(0)

  1. 暂无评论