0

In a YouTube video (https://www.youtube.com/watch?v=iLWTnMzWtj4&list=PLlasXeu85E9cQ32gLCvAvr9vNaUccPVNP&index=3), it was said that when we run the JS code, a global execution context is created. Execution context is created in two phases: Memory Creation and Code Execution phase.

In the memory creation phase var are assigned undefined and whole function body is assigned to the function name something like this:

a: undefined
foo: {whole function body}

A new Execution Context is created for each function WHEN THEY ARE INVOKED. Then the memory is assigned to its local variables.

The question is if memory is not assigned to a function local variables until it is invoked, then why do they throw SyntaxError if we make a syntax mistake in a function and the whole code doesn't run at all?

for e.g. the below code throws SyntaxError and doesn't run at all whereas the memory is allocated to foo after it is invoked which is after the first line

console.log("hello");

function foo() {
    vard world = "world";
  console.log(world);
}

foo();

Can anyone please explain when does the JS engine knows that there is actually a SyntaxError? Obviously it is not after the function is invoked because even if we don't invoke the function at all, the SyntaxError will still be thrown

Does the Global Execution Context gets created if there is a syntax error in the code? What is the sequence? Which happens first and later means the Global Execution Context gets created first or the parsing the code to machine language is done first?

2
  • Exactly when the syntax error is thrown probably depends on the engine, but it's going to be before any part of the code begins to run, as the engine is converting it to machine code. Commented Aug 26, 2021 at 21:49
  • @CharlesBamford In execution context, first memory creation phase happens then execution happens. I know that execution won't start in case of a SyntaxError. But, will the memory get allocated to the variables or the Memory Creation phase of Execution context also doesn't start either? Commented Aug 27, 2021 at 1:35

2 Answers 2

1

I am no expert on this topic but I'll throw in my two cents of knowledge.

Javascript is an interpreted language which (as you correctly stated) means that the code is run line-by-line and other errors are thrown at runtime. However as with most interpreted languages the js interpreter will run a syntax check before executing any code. This is what throws the SyntaxErrors you're seeing, the interpreter noticing code that is not valid js syntax before any of the code is executed.

Sign up to request clarification or add additional context in comments.

1 Comment

In execution context, first memory creation phase happens then execution happens. I know that execution won't start in case of a SyntaxError. But, will the memory get allocated to the variables or the Memory Creation phase of Execution context also doesn't start either?
0

That's just how a Syntax Error works. It's basically saying, this program is not valid. So it just doesn't run.

Can anyone please explain when does the JS engine knows that there is actually a SyntaxError?

Because it parses it first and while parsing the code, it finds an error which makes it stop because it doesn't know what you meant by it.

6 Comments

So what is the sequence? First code is parsed to machine code and if there are any syntax errors, it doesn't even create the execution context or the execution context is created first?
Code is parsed first. Even before any execution context is created. If your program has syntax errors it's just not run. It's just not a valid program.
The javascript engine won't be assessing which part of your code is valid and which isn't. Nor will it run only the "good" code. It doesn't make that distinction. It's either a valid program through and through or it isn't.
In execution context, first memory creation phase happens then execution happens. I know that execution won't start but if there is a SyntaxError, will the memory get allocated to correct variables or the Memory Creation phase of Execution context also doesn't start either?
No, as I understand it with a Syntax Error the program is basically unreadable.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.