Javascript runtime interview question

I would definitely put the following question in an interview, if am interested in knowing if the candidate knows a bit about the Javascript runtime.

What is the order in which the following texts are logged via console.log?

console.log(‘1 – start…


This content originally appeared on DEV Community and was authored by Adrian Matei

I would definitely put the following question in an interview, if am interested in knowing if the candidate knows a bit about the Javascript runtime.

What is the order in which the following texts are logged via console.log?

console.log('1 - start');
setTimeout(() => console.log('2 - setTimeout1'), 0);
Promise.resolve('Success')
    .then(()=> console.log('3 - promise1'))
    .then(()=> console.log('4 - promise2'));
setTimeout(() => console.log('5 - setTimeout2'), 0);
console.log('6 - end');

If you are not sure about the answer read this In depth: Microtasks and the JavaScript runtime environment article before

Output

1 - start// statement is executed directly in the script (first "Run script" task)
5 - end // part of the first "Run script" task gets executed too
3 - promise1 // placed in microTasks queue and executed between tasks, after first "Run script" task is ready
4 - promise2 // microtask added  during previous microtask execution  and executed immediately
2 - setTimeout1 // callback execution scheduled in another task in the "macroTasks" queue (also task queue), executed in the next interation of the event-loop
5 - setTimeout2 // callback execution scheduled in another task in the task queue, executed in the next iteration of event-loop

Shared ❤️ from Codever.   ?   use the copy to mine functionality to add it to your personal snippets collection.


This content originally appeared on DEV Community and was authored by Adrian Matei


Print Share Comment Cite Upload Translate Updates
APA

Adrian Matei | Sciencx (2021-08-14T18:56:59+00:00) Javascript runtime interview question. Retrieved from https://www.scien.cx/2021/08/14/javascript-runtime-interview-question/

MLA
" » Javascript runtime interview question." Adrian Matei | Sciencx - Saturday August 14, 2021, https://www.scien.cx/2021/08/14/javascript-runtime-interview-question/
HARVARD
Adrian Matei | Sciencx Saturday August 14, 2021 » Javascript runtime interview question., viewed ,<https://www.scien.cx/2021/08/14/javascript-runtime-interview-question/>
VANCOUVER
Adrian Matei | Sciencx - » Javascript runtime interview question. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/08/14/javascript-runtime-interview-question/
CHICAGO
" » Javascript runtime interview question." Adrian Matei | Sciencx - Accessed . https://www.scien.cx/2021/08/14/javascript-runtime-interview-question/
IEEE
" » Javascript runtime interview question." Adrian Matei | Sciencx [Online]. Available: https://www.scien.cx/2021/08/14/javascript-runtime-interview-question/. [Accessed: ]
rf:citation
» Javascript runtime interview question | Adrian Matei | Sciencx | https://www.scien.cx/2021/08/14/javascript-runtime-interview-question/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.