Definition of Synchronous and Asynchronous using javascript example

Janaka Chinthana Dissanayake
2 min readMar 31, 2021

Synchronous Javascript basically uses one by one method. It means if there are some methods to execute in a program, it will execute one by one. Synchronous will be happening like if the first method is starting to execute, all other methods wait until the previous method is fully completed respectively.

Example:

This is the code segment and its result

You can see there are four-string to print in the console and printed those are one by one as respectively in the above image. You can call it Synchronous.

Asynchronous Javascript is behaving like opposite of the Synchronous. If there are many methods to be executed and some methods having to get a long time to execute such as the setTimeout() function, the other methods are not waiting for the results of that. They will execute as much as possible

Example:

This is the code segment and its result

You can see there is a string (console.log(“HI… I am the Second method to be executed”);) to be print which is inside the timeout function and also it is in the second place to be executed (ignore the empty console.log() method). But it getting two seconds to complete the execution. Because of that, the result shows it as the last printed statement. All other statements are printed without waiting for the timeout function. This is can be called Asynchronous.

In the conclusion, we can simply say the Synchronous is a method that functions waiting until its previously started functions are fully completed and the Asynchronous is a method that functions are not waiting until its previously started functions are fully completed.

--

--

Janaka Chinthana Dissanayake
0 Followers

I am an undergraduate student of SLIIT following BSc.(HonS ) in IT specialization in Software Engineering