Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why browser is not throwing exception for syntax error?

Tags:

javascript

I accidentally wrote a wrong JavaScript syntax (I think so).

code is

  var temp = {};
    temp.a = 34;
    height:34, //should fail here.
    temp.b = 56;

jsfiddle

is syntax is correct?

Thanks.

like image 432
Anoop Avatar asked Sep 11 '12 11:09

Anoop


People also ask

How do you handle syntax errors?

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.

Are syntax errors exceptions?

SyntaxError is a perfectly ordinary built-in exception. It is not special in any way. Only the circumstances of when it's (usually) thrown are a bit unusual. A syntax error means that the code featuring said error cannot be parsed.

What is the main characteristic of a syntax error?

In computer science, a syntax error is an error in the syntax of a sequence of characters or tokens that is intended to be written in a particular programming language. For compiled languages, syntax errors are detected at compile-time. A program will not compile until all syntax errors are corrected.

When the browser's JavaScript engine encounters an error it will throw an?

A SyntaxError is thrown when the JavaScript engine encounters tokens or token order that does not conform to the syntax of the language when parsing code.


1 Answers

A colon can be used to label a statement, and that's what's happening here. There are no errors in your code, it's intended behavior.

Edit: better resource on labels. :)

like image 68
Elliot Bonneville Avatar answered Nov 09 '22 15:11

Elliot Bonneville