DEV Community

Cover image for How JS works?
Neeraj Kumar
Neeraj Kumar

Posted on

1

How JS works?

Everything in JS happens inside an Execution Context.

Execution Context:
Component where JS is executed. It has:
1.Memory or Variable Env
Here variables are stored as key: valuepair.
2.Code Env or Thread of execution
This is the place where code is executed one line at a time.
Note: JavaScript is a synchronous single-thread language.

1nd Phase: Memory creation phase

  • Everything happens in a ‘Global’ execution context.
  • In this phase, memory is allocated to all variables and function(s).
  • For variable(s), key is the variable name itself and value is undefined (even if the variable is initialized). And, for
    function(s), key is the function name and value is body of the
    code.
    2nd Phase: Code Execution phase

  • Code is traversed line by line and actual value of variables are assigned to them.

  • A new ‘local’ execution context is created, when function
    ‘invocation’ is encountered. Again, two phase perform their role.

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay