JavaScript Output/Console Based Interview Questions
Sandeep Tiwari
React Native || NativeScript || iOS Developer || React || TV Application || JavaScript || TypeScript || Redux || Git || Website Developer
In JavaScript interviews, candidates often encounter questions that focus on output-based scenarios, where they may encounter challenges. In this blog, I'll be discussing some of the most commonly asked JavaScript interview questions that revolve around predicting or understanding code output.
Here are the most commonly asked interview questions that focus on output.
Question1:- What output do you expect from the following code snippet?
var z = 1,
y = z = typeof y;
console.log(y);
//output
Question2:- What can be the result of the following code snippets?
console.log("10"+20+30);
console.log("10"+"20"+30);
console.log(10+20+"30");
Question3:- What will be the result, if you do console.log(3 > 2 > 1)?
console.log(3 > 2 > 1);
1. True
2. False
3. Undefined
4. Error
Question4:-
console.log([] + []);
//output
Question5:- What will be the output of the code below?
var array = ["1","2","3","4","5"];
delete array[3];
console.log(array.length);
Question6:- What will be the output in both cases?
var Employee = {
company: 'Acme'
}
var employee1 = Object.create(Employee);
delete employee1.company
console.log(employee1.company);
delete Employee.company
console.log(employee1.company);
Question7:- Are these both function f1 and f2 are same? and what will be output?
function f1()
{
return {
name: "Sandeep"
};
}
function f2()
{
return
{
name: "Sandeep"
};
}
Question8:- Is this function correct to remove duplicate values from an array or not?
领英推荐
let arr = ["Agra", "Delhi", "Agra","Mathura", "Mathura", "Gurugram"];
function removeDuplicates(arr) {
return arr. filter((item,index) => arr.indexOf(item)===index));
}
console.log(removeDuplicates(arr))
//if no, comment your easiest way to remove duplicates values from an array.
Question9:- What will be the result of below code snippet?
const val = ( function() {
const x = 10;
const y = 20;
const z = 30;
return {x,y};
})();
console.log( val.x );
console.log( val.z );
Question10:- Which function will call first or call and what will be the result of below code snippet?
function add(a, b) {
return a + b;
}
function add(a, b, c = 10) {
return a + b + c;
}
console.log(add(5, 10));
Question11:- Let's guess the output?
const arr=[1,2,3,4,5];
let sum=0;
for(let i=0;i<5;i++){
if(i%2===0){
sum+=arr[i];
}
}
console.log(sum);
Question12:- Predict the output of the below code?
function display() {
var a = b = 10;
}
display();
console.log('b', typeof b === 'undefined');
console.log('a', typeof a === 'undefined');
Question13:- Predict the output of the below codes, if we change var instead of let?
for(let i = 0; i <5; i++)
{
setTimeout(()=>{
console.log(i);
},0)
}
Similar as above question only difference let and var:-
for(var i = 0; i <5; i++)
{
setTimeout(()=>{
console.log(i);
},0)
}
Question14:- What is the difference between declaring a function in the two formats below?
foo();
function foo() {
console.log('Sandeep');
}
foo1();
var foo1 = function() {
console.log('Sandeep');
};
Question15:- What is the difference between declaring a function in the two formats below?
async function foo() {
console.log("A");
await Promise.resolve();
console.log("B");
await new Promise(resolve => setTimeout(resolve, 0));
console.log("C");
}
console.log("D");
foo();
console.log("E");
That wraps it up for this article, folks. As you know lot of questions are available in Javascript, So focus on Array, Object, String methods well. I've included various types of questions to ensure comprehensive coverage.
Please share your feedback or questions in comments section. Like, Share and Comment.
Profile Visit:- https://www.dhirubhai.net/in/sandeep-tiwari-71a9081b4/