JavaScript​: single-threaded and synchronous

JavaScript: single-threaded and synchronous

A few days ago single threaded and synchronous were just 2 heavy words for me. If this is you right now, don't worry I'll try my best to make you understand.

So, let's get started:

Single-threaded and synchronous are not that much different.

Single-threaded: It can do only 1 thing at a time and has a single call stack (don't worry just read and soon you'll get what it is)

Synchronous: As the name suggests synchronous means to be in a sequence. So, basically a function has to wait for the earlier function to get executed and everything stops until the wait is over.

The call stack is basically a data structure that records where we are in the program. If we step into a function we push it to the top of the stack and when we return a value from the function we basically pop the function off from the stack.

Let's understand it by running the below code:

function multiply(a, b){
     return a*b;
}

function square(n){
     return multiply(n)
}

function printSquare(n){
     let squared = square(n)
     console.log(squared)
}

printSquare(4)        

👇See how the above code executes: There will be a main() function when the file starts executing then we call printSquare which gets pushed over the top of the stack which in turn calls a square function which gets pushed over at the top of the stack which in turn calls the multiply function which gets pushed over at the top of the stack.

[Click to continue reading...]

Abhishek Sinha

Services IT Development Program Senior Associate | ServiceNow | IT Service Management

1y

[Click to continue reading...] button is not working...!!!

Like
Reply

To view or add a comment, sign in

More articles by Rajat Gupta

  • pseudo classes in css part 1 (:hover)

    Note: This is the first part of the series dedicated to the pseudo-classes of CSS. In this part, we'll understand the…

  • useContext in react: everything you need to know

    only understand this: Using useContext is not necessary, we use it to avoid prop drilling. Here's how prop drilling…

  • Sibling combinator in css

    There may be some confusion as what we are calling sibling. So, let's first get that out of the way.

  • rest parameter in javascript

    The rest parameter is introduced in ES6. It allows a function to accept an indefinite number of arguments as an array…

    1 Comment
  • PropTypes in react

    Let's see what reactjs.org has to say: As your app grows, you can catch a lot of bugs with type-checking.

  • What the heck are props in react

    Although we can make web apps using JavaScript. One of the reasons we are using react over JS is component reusability.

  • reduce() method in JavaScript

    Let's see what MDN has to say: The reduce() method executes a user-supplied “reducer” callback function on each element…

  • filter() method in JavaScript

    Definition by MDN: The filter() method creates a new array with all elements that pass the test implemented by the…

  • map() method in JavaScript

    Let's see what MDN has to say: The map() method creates a new array populated with the results of calling a provided…

  • object-fit property in CSS

    The object-fit CSS property sets how the content which includes images, videos and other embedded media formats should…

Insights from the community

Others also viewed

Explore topics