Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 2641

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 26, 20242024-11-26T07:18:07+00:00 2024-11-26T07:18:07+00:00

What is JavaScript ? How is JS Code executed line by line?

  • 61k

According to MDN Docs, a high level definition of JavaScript is:

JavaScript is a scripting or programming language that allows you to implement complex features on web pages.

Everything in JavaScript happens inside Execution Context. All the JavaScript code is executed inside it.

The execution context is a big box that contains two components:

  1. Memory Component/Variable Environment
  2. Code Component/Thread of Execution

Memory Component: Here all the variables and functions are stored as key-value pairs.

Code Component: Here whole JS code is executed one line at a time.

The above explanation makes us reach to this definition:
JavaScript is a Synchronous Single-Threaded language

Let's break down the above definition by understanding these keywords:
Synchronous: In a specific order
Single-threaded: JavaScript can execute one command at a time.
It can only go to the next line once the current line has been finished executing.

Now let us understand how the execution context is created with the help of JavaScript program.

var n = 2 function square(num){     var ans = num * num     return ans } var square2 = square(n) var square4 = square(4) 
Enter fullscreen mode Exit fullscreen mode

When we run this whole code, a global execution context(GEC) is created. GEC is created in two phases:

  1. Memory Creation Phase
  2. Code Execution Phase

Memory Creation Phase
In this phase JavaScript will allocate memory to all the variables and functions.
Memory allocation starts from line 1 and goes till line 7 of the above code.

  1. As JS encounters line 1 it allocates memory to n. It stores a special value as undefined.
  2. It then see a function named square and allocation memory to variable square. Here it stores the whole code of the function inside this memory space.
  3. Similary it will allocate memory to square2 and square4 as undefined.
Memory Component Code Component
n: undefined
square: (num){
var ans = num * num
return ans
}
square2: undefined
square4: undefined

Code Execution Phase
The code again runs from line 1 to 7 after the memory allocation phase is finished.

  1. JS encounters line 1 and replaces n's value from undefined to 2
  2. Now from line 2 to 5 there is nothing to execute so it moves to line 6
  3. At line number 6 function is invoked, since function is a new mini program in itself so a new Execution Context is created.
    New execution context again has memory component and code component.

    1. Memory Allocation Phase

      1. It allocates memory to num: undefined
      2. It allocates memory to ans: undefined
      3. No more functions or variables so memory allocation phase is completed.
      Memory Component Code Component
      num: undefined
      ans: undefined
    2. Code Execution Phase

      1. When function is invoked with argument n then in line 1 n=2 which is passed as parameter to the function
      2. In the next line num * num is calculated inside code component and put inside ans in memory component.
      3. Now it sees return ans. When JS encounters return it returns the control back to the execution context where the function was invoked.
      4. Now the control goes line 6 again.
      5. The whole execution context for this instance of square function will now be deleted. alt text
  4. Now the square2 variable's undefined will be replaced with 4 (retuned as ans)

  5. Similary function will be invoked on line 7 and pass 4 as argument which will return 16 (with all the above steps repeating in the same order).

Image description

Now JS is done with executing the whole code, now the whole Global execution context is deleted.
This is how JavaScript code is executed line by line.

Thanks!

beginnersbrowserjavascriptwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.