1

I am just reading Kyle's "You don't know JS" book series/watch his lectures and he says, that for following code:

var a = 2;
2();

Javascript language is concerned, line two is an error. It's actually syntactically invalid. It's not a runtime problem; it's an actual authortime problem.

So in interpreted language if we was purely running line by line and we weren't doing multiple passing - we'd run line one first, and then we would discover that line two had a problem, and we'd throw an error.

But a compiled language, would say line two's problem, and it would flag you with that error before it ever tried to run line one.

"So JavaScript, in that respect, is more a compiled language than an interpreted language, because JavaScript definitely does look at line two first before it's ever tried to run line one. It looked at line one to understand what line one was about, but it didn't run it. So when you put a program like this, if that was in a file and you loaded that up into a browser, line one would never run. You'd immediately get an error saying line two is invalid."


So i tried this code in chrome's console and surprisingly it works fine, i mean a variable is going to be assigned, and if we try later console.log(a) it prints "2".

Is it some kind of console's specific behavior or what?

Grigoryants Artem
  • 1,401
  • 2
  • 15
  • 32

1 Answers1

1

The author is wrong.

First: his example is invalid.

2() is -- perhaps a bit surprisingly -- not a syntax error in Javascript! It's perfectly valid syntax; it's just not a thing that you can do, so it generates a type error at runtime:

> console.log("test"); 2()
test
TypeError: 2 is not a function

This sort of error doesn't prevent the program from being compiled and run. As you can see in the transcript, the error is only thrown when the invalid call is reached; other code before it runs normally.

Compare what happens when we introduce a real syntax error:

> console.log("test"); 1x
SyntaxError: Unexpected identifier

This is actually a syntax error -- a "word" starting with 1 is assumed to be a number, and numbers can't have x in them -- so it prevents the entire expression from being run. The console.log() is never executed.


Second: the author is drawing the line between "interpreted" and "compiled" languages in the wrong place.

Parsing a program to figure out what it means, and to catch any syntax errors, is not the same as "compiling" it. It is a necessary first step in compilation, but it's not the whole thing. In reality, "compiled" languages are generally defined as ones where an input program is converted to a native executable before it is run. In this respect, Javascript is clearly an interpreted language.

If we were to adopt the author's definition of "interpreted languages" as ones where a program is run in a single pass, without even parsing the rest of the file, there would be almost no languages in that category. (The only ones that come to mind are some shell scripting languages.)