Showing posts with label JavaScript. Show all posts
Showing posts with label JavaScript. Show all posts

Call Stack

JavaScript is a single-threaded programming language, which means it has a single call stack. Having a single call stack means that it can only handle one task at a time.

A call stack is a data structure that records where we are in the program.

Its operation is as follows: When a function is executed, its record is added (pushed) to the top of the stack. 

When we return a result from the function, the function that was previously on the stack is removed.

For example:

function multiply(x, y) {
    return x * y;
}
function printSquare(x) {
    var s = multiply(x, x);
    console.log(s);
}
printSquare(5);
 

 

8. Javascript의 콜 스택과 이벤트 루프 :: Front J 

In a web browser, each time an event occurs, a message is added and an event listener is attached. 

Therefore, if there is no listener, the event will be lost. 

Callback function invocation is used as the initial frame of the call stack, and since JavaScript is a single-threaded language, all polling and processing of messages are stopped until all calls on the stack are returned. 

Synchronous function calls, on the other hand, add a new call frame to the stack, which is the opposite of asynchronous callback function invocation.자바스크립트 호출 스택(Call Stack) 이해하기