Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't this crazy brackety Javascript cause a syntax error?

Tags:

javascript

I was playing around with some Javascript snippets today and noticed that this code would actually run:

{{for(var i = 0; i < 3; i++) {{{{
  alert(i);
}}}}}}

You can try it out for yourself on jsFiddle.

Why does this run without any syntax errors? What do the repeated brackets mean? Does Javascript just ignore repeated curly braces?

like image 879
Peter Olson Avatar asked Nov 30 '22 14:11

Peter Olson


1 Answers

It creates a new block, which is effectively useless1 because JavaScript doesn't have block scope2.

1 This is a beautiful oxymoron.
2 Yet.

like image 155
Ry- Avatar answered Dec 02 '22 02:12

Ry-