2

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

7. JavaScript ignores extra spaces: ◉ True ✗ — ⭕ False ✓

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

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
Cloudboy22
  • 1,496
  • 5
  • 21
  • 39
  • @jfriend00 no the question was this only.. – Cloudboy22 Jan 27 '16 at 07:23
  • @Xufox please tell me how is it wrong.. – Cloudboy22 Jan 27 '16 at 07:25
  • It was quite unclear that you were referring to a question in some quiz, rather than a quiz you were making. That question however doesn’t provide enough context. JavaScript ignores spaces _where_? Within a string? No. Before a dot in dot notation (e.g. `[] .slice()`)? Yes. Where did you find this quiz? – Sebastian Simon Jan 27 '16 at 07:28
  • 3
    That's a particular BAD quiz. Ambiguous wording to many questions - even someone who knows a LOT of Javascript. – jfriend00 Jan 27 '16 at 07: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 Jan 27 '16 at 07: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](https://developer.mozilla.org/en/). – Sebastian Simon Jan 27 '16 at 08:22
  • 1
    I'm starting to believe that when the quiz writes "Java Script", they really did not mean JavaScript. – Bergi Jan 27 '16 at 09: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. –  Jan 27 '16 at 09:29
  • After four years, I decided to take this “quiz” again. Geez, it’s so awful… In 2021, [the answers I’ve clicked on](https://i.stack.imgur.com/O3Cku.png) are the correct ones, justifiable by the ECMAScript specification, documentation, my 13 years of JS experience, and — to be fair — a fair amount of pedantry. If multiple answers applied, I clicked the first one that did (doesn’t look like the quiz shuffles options). Questions that weren’t answered either imply “none of the above” or the question doesn’t make sense. Not even half the questions are correct. Can’t believe this site still exists. – Sebastian Simon Mar 24 '21 at 00:00

2 Answers2

3

I seriously wouldn’t trust that site…

Directly conflicting answers

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.

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
1

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
Community
  • 1
  • 1
Thilo
  • 257,207
  • 101
  • 511
  • 656