-2

I was goofing around with my friends and throwing together random codes when I noticed something strange. I was setting a variable to 4 but I accidentally hit enter instead of space. It looked like this:

var                                     //note there are no semicolons
example = 4

when I called it, it still returned 4.

why did this happen and how can I use this to my advantage?

a2800276
  • 3,272
  • 22
  • 33
DJHakim
  • 133
  • 1
  • 11
  • It happened because `var` does not expect you to end the statement there anyhow. Javascript does automatic semicolon insertion and is smart enough to figure out where to end your statement. There is no known advantage. – somethinghere Dec 16 '15 at 15:06
  • Can't see any possible advantage of this at all personally. – SK2017 Dec 16 '15 at 15:06

1 Answers1

0

JavaScript is not whitespace-delimited. It typically doesn't care whether you have spaces or line breaks in your code. Also, semicolons are optional when there's only one statement in a block, though most developers prefer to use them for readability and consistency.

isherwood
  • 58,414
  • 16
  • 114
  • 157