Table of contents
Introduction
Namaste, In this blog I will discuss 10 code scenarios on Javascript .
Identify the output of the below program
async function* fx() { yield await Promise.reject(1); } let genobj = fx(); genobj.next().then(v => console.log("Success",v), v => console.log("Reject",v));
a.Reject 1
b.undefined
c.error
d.true
e.Success 1
Answer: a
Reason: The rejected value is 1.
Identify the output of the below program
let obj = { name:"ram", age:30, salary:1000 }; let {name, ...rest} = obj; console.log (name);
a.undefined
c.ram
d.error
e.null
Answer:c
Reason:The value name variable has is ram.
Identify the output of the below program
let obj = { age:30, salary:1000 }; let {age, ...rest} = obj; console.log(rest);
a.{age:30}
b.{salary:1000}
c.error
d.undefined
e.salary
Answer:b
Reason:The variable rest has value is {salary:3000}.
Identify the output of the below program
let obj = { age:30, salary:1000 }; let {...rest, age} = obj; console.log(rest);
a.{age:30}
b.{salary:1000}
c.undefined
d.error
e.null
Answer:d
Reason:The syntax is invalid.
Identify the output of the below program
let obj = { name:"ram", }; let obj2 = { name2:"gopi", }; let obj4 = {...obj, ...obj2}; console.log(obj4);
a.{name:'ram', name2:'gopi'}
b.error
c.undefined
d.{name:'ram'}
e.{name2:'gopi'}
Answer:a
Reason: Both the objects are merged into obj4.
Identify the output of the below program
let prm = new Promise((resolve, reject) => { setTimeout(() => { reject("Rejected"); }, 1000); }); prm.then( (data) => { console.log(data); }, (error) => { } ).finally(() => { console.log("Finally"); });
a.Rejected
b.Error
c.Finally
d.undefined
e.null
Answer:c
Reason:The finally block is executed.
Identify the output of the below program
let obj = { name:"ram", }; let obj3 = {...obj}; console.log(obj3);
a.{name:'ram'}
b.ram
c.undefined
d.error
e.null
Answer:a
Reason:The spread operator creates a shallow copy of obj.
Identify the output of the below program
let prm = new Promise((resolve, reject) => { setTimeout(() => { resolve("Resolve"); }, 1000); }); prm.then( (data) => { }, (error) => { } ).finally(() => { console.log("Finally"); });
a.Resolve
b.Finally
c.Error
d.undefined
e.null
Answer:b
Reason:The finally() function when executed it prints finally.
Identify the output of the below program
let obj = { name:"ram", age:30, salary:1000 }; let obj3 = {...obj}; if (obj3 == obj) console.log("same"); else console.log("different");
a.same
b.different
c.no output
d.undefined
e.error
Answer: b
Reason: Both objects are not same i.e located at different locations in memory.
Identify the output of the below program
let obj = { age:30, salary:1000 }; let {...rest, ...rest2} = obj; console.log(rest);
a.{age:30}
b.{salary:1000}
c.undefined
d.null
e.error
Answer:e
Reason:The rest syntax is invalid.