JS Practice Coding Questions – Part 1

In this series, i will share the questions for practice in JS.

1. Given the following code. what will be the output

const obj = { 
 name: 'shashank', 
 address: {
     city: 'xyz'
 }
};

const a = {...obj};
a.address.city = 'delhi'

console.log(obj.address.city); // what is the output 
JavaScript

Output:

delhi

2. Given the following code. What will be the output

console.log('start')
setTimeOut(() => { 		 
 console.log('setTimeout');
 }, 0);

Promise.resolve().then(() => {
	console.log('Promise');
});

console.log('end');
process.nextTick(() => console.log('ok');
JavaScript

Output

Start
End
ok
Promise
setTimeout

3. Given the following code, what will be the output

for (var i = 0; i < 4; i++) {
  setTimeout(() => console.log(i), 0);
}
JavaScript

Output:

4
4
4
4

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

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