Javascript is a single threaded programming language, that means it only has ONE call stack and ONE memory heap which is responsible for ONE task at ONE time. If it only does ONE task per ONE time, it is such a big waste of time and resources! And yes, asynchronous is the rescuer.
console.log('1');setTimeout(() => {console.log('2');}, 3000);console.log('3');
For the sample code above, console.log('2')
will be added in the callback queue and waiting to be pushed into call stack after setTimeout
is completely executed in Web Api.
As soon as event loop checks that the call stack is empty and there is something in the callback queue, it will place that statement in the call stack and that is the moment where console.log('2')
will be executed.
Okay. At this point, you should agree that Javascript is a single threaded programming language and it supports ASYNC.
Why it is popular?
- It runs everywhere
JavaScript can run everywhere in both frontend and backend development of web and mobile applications. - Full-stack development by modern framework
- React, Vue, Angular are the modern frontend framework used for Javascript.
- NestJs, ExpressJs are the modern backend framework used for NodeJs. - Flexibility
- typescript is the superset of JS, static type…