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

whitespace - JavaScript ignores extra spaces? - Stack Overflow

programmeradmin2浏览0评论

In this JavaScript quiz at WsCube Tech there was a question whether JavaScript ignores extra spaces. The correct answer was “False”.

Isn’t JavaScript white-space independent? I have read in many blogs that it is. So why is my answer wrong?

In this JavaScript quiz at WsCube Tech there was a question whether JavaScript ignores extra spaces. The correct answer was “False”.

Isn’t JavaScript white-space independent? I have read in many blogs that it is. So why is my answer wrong?

Share Improve this question edited Jun 29, 2016 at 4:36 Sebastian Simon 19.5k8 gold badges61 silver badges84 bronze badges asked Jan 27, 2016 at 7:21 Cloudboy22Cloudboy22 1,5145 gold badges22 silver badges41 bronze badges 9
  • 3 That's a particular BAD quiz. Ambiguous wording to many questions - even someone who knows a LOT of Javascript. – jfriend00 Commented Jan 27, 2016 at 7:35
  • 2 For example, the quiz has the wrong answer to question 6. C-style block-level scoping is not supported in Java script. Javascript now has let which is block level scoped. – jfriend00 Commented Jan 27, 2016 at 7:38
  • 1 This quiz is just horrible and outdated and promotes bad practice. Close that page immediately and go somewhere else, where you’re safe, like on MDN. – Sebastian Simon Commented Jan 27, 2016 at 8:22
  • 1 I'm starting to believe that when the quiz writes "Java Script", they really did not mean JavaScript. – Bergi Commented Jan 27, 2016 at 9:28
  • 2 The people who wrote the quiz don't even know how to spell the name of the language--in many places they say "Java script". RUN, DON'T WALK AWAY FROM THIS QUIZ. – user663031 Commented Jan 27, 2016 at 9:29
 |  Show 4 more ments

2 Answers 2

Reset to default 3

I seriously wouldn’t trust that site…

That’s the short, sufficient answer I could give, but I’d like to say two other things:

Firstly, JavaScript doesn’t ignore spaces within strings:

var str = "Hello                World";

This string has 16 spaces and they won’t get ignored just like that. However, in-between some operators, keywords and tokens, JavaScript does ignore spaces:

var   test  = [ 0 , 1  ,     3    ]       .  slice  (      2  )      ;

This line is parsed as

var test=[0,1,3].slice(2);

Still, the space between var and test isn’t ignored. Not all spaces are equal. This quiz question cannot be answered in its current form — well, or two forms…

Secondly, that quiz has a lot of inconsistencies, false information, outdated information and promotes bad practice. I’ve just sent them a huge list of things wrong with the quiz…

It’s much safer to stick to a more “trusted” site like the Mozilla Developer Network.

For one thing, you can terminate expressions by a new line.

var x = 1  // no semicolon
console.info(x)

Also look at this (which returns undefined):

return
   12
发布评论

评论列表(0)

  1. 暂无评论