DEV Community

Fatima Alam
Fatima Alam

Posted on • Edited on

JS Output Interview Questions

Q1.

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
Enter fullscreen mode Exit fullscreen mode

Q2.

let x = 1;
if (function f() {}) {
  x += typeof f;
}

console.log(x);
//Output:
//1undefined
Enter fullscreen mode Exit fullscreen mode

Q3.

let num = 0;

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

console.log(test());
console.log(num);
//Output:
//1
//0
Enter fullscreen mode Exit fullscreen mode

Q4.

const x = 10;

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

foo();
//Output:
//Uncaught 
//ReferenceError: Cannot access 'x' before initialization
Enter fullscreen mode Exit fullscreen mode

Q5.

function foo() {
  let x = 1;

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

  bar();
}

foo();
//Output:
//3
Enter fullscreen mode Exit fullscreen mode

Q6.

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

Q7.

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
Enter fullscreen mode Exit fullscreen mode

Q8.

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

console.log(a);
console.log(b);
console.log(c);
//Output:
//1
//2
//3
Enter fullscreen mode Exit fullscreen mode

Q9. Is there any mitsake

function sayHello() {
    setTimeout(function() {
        console.log('Hello')
    }, 1000)
}
//Output:
//function only defined, not called
Enter fullscreen mode Exit fullscreen mode

Q10.

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

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

Q11.

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

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

Q12.

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!
Enter fullscreen mode Exit fullscreen mode

Q13.

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
Enter fullscreen mode Exit fullscreen mode

Q14.

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}
Enter fullscreen mode Exit fullscreen mode

Q15.

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)
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
hendrikras profile image
Hendrik Ras

Hey friend, nice post! 👋

You might want to double-check your formatting in this post, it looks like some things didn't come out as you intended. Here's a formatting guide in case you need some help troubleshooting. Best of luck and thanks again for sharing this post!