[SOLVED] getting different output in unordered manner

Issue

I am new to javascript so please do answer in detail,
I am calling settimeOut first and than calling an anonymous functions
but the out is arranged in opposite manner,
So What i want to know why is it so,is it because of online compiler or any concept
This is the imageimage

setTimeout(function() {
    console.log('Execute later after 1 second')
}, 1000);

let show = function() {
    console.log('Anonymous function');
};

show();

Solution

That is due to what setTimeout does. setTimeout receives a callback function and a timeout in milliseconds, the execution of the callback function will be delayed by the given amount of the timeout. Meaning that show() will be executed first and console.log("Execute later after 1 second"); after one second.
Long story short setTimeout delays the execution of a given function.
So everything works as it should.

Answered By – Palladium02

Answer Checked By – Marie Seifert (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *