Fun Puzzles JavaScript dated-01/03/2023

Hello Everyone!
It’s time for fun Puzzle!

Here I have attached some fun Puzzles related to CallStack | Promises:

Please execute and reply your answer!

1)What will be the output of the following code?

function foo() {
  console.log("Hello");
}

function bar() {
  foo();
  console.log("World");
}

bar();

2)What will be the output of the following code?

console.log("Start");

new Promise((resolve, reject) => {
  console.log("Promise executor");
  resolve("Promise resolved");
}).then((result) => {
  console.log(result);
});

console.log("End");

3)What will be the output of the following code?

console.log("Start");

Promise.resolve("Promise resolved").then((result) => {
  console.log(result);
});

console.log("End");

4) What will be the output of the following code?

var x = 10;

function foo() {
  console.log(x);
  var x = 20;
}

foo();

5)What will be the output of the following code?


function foo(callback) {
  setTimeout(() => {
    console.log("Hello");
    callback();
  }, 0);
}

function bar() {
  console.log("World");
}

foo(bar);

Have a Happy Learning
Thanks & Regards
Teaching Assistant,
Dharshini M.

1 Like
  1. Hello
    World
    2.Start
    Promise executor
    End
    Promise resolved
    3.Start
    End
    Promise resolved
    4.undefined
    5.Hello
    World
  1. Hello World
    2.Start
    Promise executor
    End
    Promise resolved
    3.Start
    End
    Promise resolved
    4.undefine
  2. World
    Hello

@Amaragarwal7 @jayachalla8 Thank you for your response :grinning:
The correct answer is

  1. Hello World
    2.Start
    Promise executor
    End
    Promise resolved
    3.Start
    End
    Promise resolved
    4.undefine
  2. World
    Hello

Keep Learning!

  1. " Hello"
    “world”
    2." Start"
    “Promise executor”
    " End "
    “Promise resolved”
    3." Start"
    “End”
    “Promise resolved”
  2. Undefined
    5.Hello
    World

@bhurep178 Thank you for your response!
Please check the correct answers.
Have a Happy Learning :grinning: