Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • What is javascript engine?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 676
    Comment on it

    There are several different implementations of javascript engines by far the most popular engine is Google's V8( Which is not only limited to client side but also with the server side with NodeJS).

    But what is actually does? It's actual job is to go through all the lines of code of an application and process them one at a time. That is means javascript is single-threaded.

    So, if you are running a line of code that take a longgg time, then the lines after it will be blocked. We don't write the code i.e., blocking. For ex:- If user clicks on a button and nothing happens browser hangs.

    So, how js engine know how to process a single line of code at a time? It used Call Stack.

    Call Stack works in (Last In First Out) L.I.F.O mechanism. It mean the process which comes executed first.

    Let's go with some practical examples:-
    main.js

    firstFunction() {
    console.log(I am first function);
    }
    secondFunction() {
    firstFunction();
    console.log(I am second function);
    }
    secondFunction();
    /*
    Result is:  
    I am first function
    I am second function
    
    */

    And here is the sequence in call stack:­

    1) Initial State:

     

    main.js

    2) SecondFunction() is invoked:

     

    secondFunction()

    3) Invoking SecondFunction() caused firstFunction() invoked:

     

    firstFunction()

    secondFunction()

    Executing firstFunction causes the string “I am first function” to be logged out first and since there are no more lines to execute in firstFunction, firstFunction is removed from the call stack:

     

    After first function returns:

     

    secondFunction()

     

    firstFunction() Removed from the stack

     

    I am first function

    Execution continues in secondFunction and “I am second function” is logged out. Once the string is logged out though, there are no more lines to execute in secondFunction and secondFunction is removed from the call stack:

    After second function returns:

     

    main.js

                          

    secondFunction() Removed from the stack

     

    I am first function

    I am second function

    Finally, since there are no more lines of code to execute in main.js, main.js is removed from the call stack as well:

     

    After main.js returns:

     

    Memory cleared.

    Console:

    I am first function

    I am second function

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: