Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript: {}==false is a SyntaxError?

In Firefox 3.5, I type this in the Firebug console :

false=={} // => evals to false
{}==false // syntax error

What is the explanation for this ?

like image 337
glmxndr Avatar asked Oct 02 '09 13:10

glmxndr


People also ask

What is a syntax error JavaScript?

An exception caused by the incorrect use of a pre-defined syntax. Syntax errors are detected while compiling or parsing source code. For example, if you leave off a closing brace ( } ) when defining a JavaScript function, you trigger a syntax error.

How to check syntax error in JavaScript?

You can click the scripts tab to review page scripts. FireFox : Install firebug and run it to get a similar console to the above stated chrome utilities. IDE : You may wish to use netbeans or Eclipse which both offer syntax highlighting and code completion for JavaScript. These will indicate syntax errors at code time.

How to Handle the syntax error?

How to Handle SyntaxError. Syntax errors in Javascript cannot be handled by using try-catch blocks as they are thrown while the code is being parsed. The window. onerror() function can be used instead to figure out that there is a syntax error.


1 Answers

{

at the start of a statement signals a ‘statement block’ (see ECMA-262-3 section 12.1), which contains a list of statements.

}

immediately ends the statement block with no statements in it. That's fine. But now the parser is looking for the next statement:

==false

Huh? That's not a statement; syntax error.

What are statement blocks for? Well, you are writing a statement block every time you say:

if (something) {
    ...
}

JavaScript defines these flow-control statements as:

if "(" <expression> ")" <statement> [else <statement>]

ie. in the single statement form with no braces. It then allows you to use a statement-block anywhere you can use a single statement, which means you can have if-braces-many-statements. But it also means you can have a statement-block on its own with no associated flow-control statement.

This serves absolutely no practical purpose! You might be tempted to think it gave you information-hiding, but no:

var a= 1;
{
    var a= 2;
}
alert(a);

...results in 2, because statement blocks don't in themselves create a new scope.

JavaScript defines flow control and statement blocks in this manner because C (and other languages derived from it) did. Those languages didn't make {} serve double-duty as an Object literal expression though, so they didn't have the ambiguity that makes this another JS misfeature.

Even this wannabe-literal:

{
   a: 1
}

is a valid statement block, because ‘:’ is used to denote a label in a statement. (and 1 is a useless expression-statement, with the semicolon omitted.) Labels are another feature inherited from C that are rarely used in JavaScript. They're not totally pointless like the blocks, but they're seldom needed and often considered in poor taste.

(A literal with two properties will cause a syntax error, as object literals use comma separators, but labelled statements must be separated by semicolons.)

This is not the only place where JavaScript's loose syntax can trip you up by making some other statement of something you think is an expression.

like image 187
bobince Avatar answered Nov 08 '22 21:11

bobince