JS Output Interview Questions
Published: 2024-06-12 07:06:21


Q1. 
```JS
let obj1 = { key: "value" };
let obj2 = obj1;
let obj3 = obj2;

obj1.key = "new value";
obj2 = { key: "another value" };

console.log(obj1.key, obj2.key, obj3.key);

//Output:
//new value another value new value
```



Q2. 
```JS
let x = 1;
if (function f() {}) {
  x += typeof f;
}

console.log(x);
//Output:
//1undefined
```



Q3. 

```JS
let num = 0;

function test() {
  var num = 1;
  return num;
}

console.log(test());
console.log(num);
//Output:
//1
//0
```



Q4. 

```JS
const x = 10;

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

foo();
//Output:
//Uncaught 
//ReferenceError: Cannot access 'x' before initialization
```



Q5.

```JS
function foo() {
  let x = 1;

  function bar() {
    let y = 2;
    console.log(x + y);
  }

  bar();
}

foo();
//Output:
//3
```



Q6.

```JS
let x = true;
setTimeout (()=>{
x= false;
}, 0);
while (x)
{
console.log(`hello`);
}
//Output:
//Hello * INFINITELY
```


Q7.

```JS
let x = true
let count =0;
setTimeout(()=>{
x= false;
},2000)
let id = setInterval(()=>{
if(x)
{
console.log(count++);
}
},200)

clearInterval(id);
//Output: 
//0
//1
//2
//3
//4
//5
//6
//7
//8
//9
```



Q8. 

```JS
const arr = [1, 2, 3];
const [a, b, c] = arr;

console.log(a);
console.log(b);
console.log(c);
//Output:
//1
//2
//3
```


Q9. Is there any mitsake

```JS
function sayHello() {
    setTimeout(function() {
        console.log('Hello')
    }, 1000)
}
//Output:
//function only defined, not called
```



Q10. 

```JS
var promise = new Promise(function(resolve, reject){
    setTimeout(function() {
        resolve('Resolved!');
    }, 1000);
});

promise.then(function(value) {
    console.log(value)
});
//Output:
//Promise {<pending>}
//Resolved!
```



Q11. 

```JS
const promise = new Promise(function(resolve, reject) {
    setTimeout(function() {
        reject('Rejected!');
    }, 1000);
})

promise.catch(function(value) {
    console.log(value);
});
//Output:
//Promise {<pending>}
//Rejected!
```



Q12. 

```JS
const promise = new Promise(function(resolve, reject) {
    resolve('Promise has been resolved!');
});

promise.then((value) => console.log(value));
console.log('I am not the promise');
//Output:
//I am not the promise
//Promise has been resolved!
```

Q13. 

```JS
const delay = () => {
    return new Promise((resolve, reject) => {
        return setTimeout(() => {
            resolve('abc');
        }, 1000)
    });
}
const sayHello = (value) => {
    console.log(value);
}
delay().then(sayHello);
//Output:
//Promise {<pending>}
//abc
```



Q14. 

```JS
const secondPromise = new Promise((resolve, reject) => {
    resolve('Second!');
});

const firstPromise = new Promise((resolve, reject) => {
    resolve(secondPromise);
})

firstPromise
    .then(promise => promise)
    .then(value => console.log(value));
//Output:
//Second!
//Promise {<fulfilled>: undefined}
```



Q15.  

```JS
const fakePeople = [
  { name: 'Rudolph', hasPets: false, currentTemp: 98.6 },
  { name: 'Zebulon', hasPets: true, currentTemp: 22.6 },
  { name: 'Harold', hasPets: true, currentTemp: 98.3 },
]

const fakeAPICall = (i) => {
    const returnTime = Math.floor(Math.random * 1000);
    return new Promise((resolve, reject) => {
        if (i >= 0 && i < fakePeople.length) {
            setTimeout(() => resolve(fakePeople[i], returnTime));
        } else {
            reject({message: "Index is out of range"});
        }
    });
};

const getAllData = () => {
    Promise.all([fakeAPICall(0), fakeAPICall(1), fakeAPICall(2)]).then(values => {
        console.log(values);
    });
}

getAllData();
//Output:
// (3) [{…}, {…}, {…}]
// 0
// : 
// {name: 'Rudolph', hasPets: false, currentTemp: 98.6}
// 1
// : 
// {name: 'Zebulon', hasPets: true, currentTemp: 22.6}
// 2
// : 
// {name: 'Harold', hasPets: true, currentTemp: 98.3}
// length
// : 
// 3
// [[Prototype]]
// : 
// Array(0)
```