This document contains 21 JavaScript interview questions related to topics such as:
1. The difference between undefined and not defined.
2. Examples testing output for functions, closures, and object properties.
3. Private methods, emptying arrays, checking array types, and function hoisting.
4. Operator behavior, typeof, delete, and instanceof.
5. Calculating the length of an associative array.
The document encourages checking answers and provides links to detailed explanations for each question.
2. What is the difference between undefined
and not defined in JavaScript?
Question 1:
3. What will be the output of the code below?
var y = 1;
if (function f(){}) {
y += typeof f;
}
console.log(y);
Question 2:
4. What is the drawback of creating
true private methods in JavaScript?
Question 3:
5. What is a “closure” in JavaScript?
Provide an example.
Question 4:
6. Write a mul function which will produce the following
outputs when invoked:
javascript console.log(mul(2)(3)(4)); // output : 24
console.log(mul(4)(3)(4)); // output : 48
Question 5:
7. How do you empty an array in JavaScript?
Question 6:
8. How do you check if an object is an array or not?
Question 7:
9. What will be the output of the following code?
var output = (function(x){
delete x;
return x;
})(0);
console.log(output);
Question 8:
10. What will be the output of the following code?
var x = 1;
var output = (function(){
delete x;
return x;
})();
console.log(output);
Question 9:
11. What will be the output of the following code?
var x = { foo : 1};
var output = (function(){
delete x.foo;
return x.foo;
})();
console.log(output);
Question 10:
12. What will be the output of the following code?
var x = { foo : 1};
var Employee = {
company: 'xyz'
}
var emp1 = Object.create(Employee);
delete emp1.company
console.log(emp1.company);
Question 11:
14. What will be the output of the following code?
Question 13:
var trees = ["xyz","xxxx","test","ryan","apple"];
delete trees[3];
console.log(trees.length);
15. What will be the output of the following code?
var bar = true;
console.log(bar + 0);
console.log(bar + "xyz");
console.log(bar + true);
console.log(bar + false);
Question 14:
16. What will be the output of the following code?
var x = { foo : 1};
var z = 1, y = z = typeof y;
console.log(y);
Question 15:
17. What will be the output of the following code?
// NFE (Named Function Expression
var foo = function bar(){ return 12; };
typeof bar();
Question 16:
18. What is the difference between the following two
functions?
Question 17:
var foo = function(){
// Some code
};
function bar(){
// Some code
};
20. What will be the output of the following code?
Question 19:
var salary = "1000$";
(function () {
console.log("Original salary was " + salary);
var salary = "5000$";
console.log("My New Salary " + salary);
})();
21. What is the instanceof operator in JavaScript?
What would be the output of the code below?
Question 20:
function foo(){
return foo;
}
new foo() instanceof foo;
22. If we have a JavaScript associative array
Question 21:
var counterArray = {
A : 3,
B : 4
};
counterArray["C"] = 1;
How can we calculate length of its counterArray?
23. Did you know how to answer all 21 questions?
Feel free to check your answers
& view detailed explanations here!