give me a few tricky js interview questions
- User
give me a few tricky js interview questions
- Assistant
- User
some more
- Assistant
give me a few tricky js interview questions
some more
Sure! Here are a few tricky JavaScript interview questions to challenge your understanding:
Question: What will be the output of the following code? Why?
console.log(0.1 + 0.2 === 0.3);
Answer: false
0.1 + 0.2 results in 0.30000000000000004 due to how numbers are represented in binary.Question: Explain the difference between var, let, and const.
Answer:
var declarations are function-scoped or globally-scoped and are hoisted to the top of their scope with their value initialized to undefined. It can be re-declared and updated.let declarations are block-scoped, not hoisted like var (temporal dead zone), and cannot be re-declared within the same scope but can be updated.const declarations are block-scoped, not hoisted like var (temporal dead zone), and cannot be re-declared nor updated (the reference cannot change for objects and arrays).Question: What will be the output of the following code? Why?
(function() {
var a = b = 3;
})();
console.log(typeof a); // ?
console.log(typeof b); // ?
Answer:
typeof a; // "undefined"typeof b; // "number"var a = b = 3; actually declares a within the function scope but assigns b to the global scope. So a is not available outside the function, but b is.Question: Can you explain how the this keyword works in JavaScript?
Answer:
this depends on the context in which it is used.
this refers to the global object (window in browsers).this refers to the object that invoked the function (in strict mode, this is undefined).this refers to the object the method is called on.this refers to the newly created object.this is lexically bound, which means it uses this from the enclosing execution context.Question: What will be the output of the following code? Why?
let a = {};
let b = { key: 'b' };
let c = { key: 'c' };
a[b] = 123;
a[c] = 456;
console.log(a[b]);
Answer:
456[object Object] in this case). So, a[b] and a[c] are both effectively a['[object Object]']. The last assignment wins, so a[b] is 456.Question: What is the output of the following code? Explain.
function foo() {
return {
bar: "hello"
};
}
function baz() {
return
{
bar: "hello"
};
}
console.log(foo());
console.log(baz());
Answer:
console.log(foo()); // { bar: "hello" }console.log(baz()); // undefinedbaz(), JavaScript's automatic semicolon insertion inserts a semicolon right after the return statement, causing it to return undefined. The block after the return statement is therefore considered unreachable code.These questions cover a range of topics and edge cases, including type coercion, variable scope, the this keyword, and more. Understanding these concepts deeply will give you a strong foundation in JavaScript.
Of course! Here are a few more tricky JavaScript interview questions:
Question: What will be the output of the following code? Why?
console.log(typeof null);
console.log(null instanceof Object);
Answer:
typeof null // "object"null instanceof Object // falsetypeof null returns "object" due to a historical bug in JavaScript that has never been corrected to maintain backward compatibility.null is not an instance of Object, so null instanceof Object returns false.Question: What will be the output of the following code? Why?
var x = 21;
var girl = function() {
console.log(x); // ?
var x = 20;
};
girl();
Answer:
undefinedgirl function, var x is hoisted to the top of the function scope and initialized with undefined, so the console.log statement will log undefined.Question: What will be the output of the following code? Why?
function sayHi() {
console.log(name);
console.log(age);
var name = 'John';
let age = 25;
}
sayHi();
Answer:
undefinedReferenceError: Cannot access 'age' before initializationname variable is hoisted and initialized with undefined. However, the age variable is declared with let, which is hoisted but not initialized, which means it is in the "temporal dead zone" and cannot be accessed before its declaration.Question: What will be the output of the following code? Why?
for (var i = 0; i < 3; i++) {
setTimeout(function() {
console.log(i); // ?
}, 1000);
}
Answer:
333var keyword does not create a new scope within the loop, so the same variable i is shared across all iterations. By the time the setTimeout callbacks execute, the loop has finished, and i is 3.Question: What will be the output of the following code? Why?
var arr = [1, 2, 3];
arr[10] = 99;
console.log(arr.length); // ?
Answer:
11arr[10] = 99 makes the length of the array 11.**Question: How can you implement a function add so that the following code works?
console.log(add(2)(3)(4)); // 9
Answer:
You can implement it using currying and closures:
function add(a) {
return function(b) {
return function(c) {
return a + b + c;
};
};
}
Question: What is the difference between Function.prototype.call and Function.prototype.apply?
Answer:
Function.prototype.call allows you to invoke a function with a given this value and arguments provided individually.Function.prototype.apply allows you to invoke a function with a given this value, but the arguments are provided as an array (or array-like object).function greet(greeting, name) {
console.log(greeting + ', ' + name);
}
greet.call(null, 'Hello', 'Alice'); // "Hello, Alice"
greet.apply(null, ['Hello', 'Alice']); // "Hello, Alice"
Question: What will be the output of the following code? Why?
var obj = { a: 1 };
(function(obj) {
obj = { a: 2 };
})(obj);
console.log(obj.a); // ?
Answer:
1obj parameter inside the IIFE is a copy of the reference to the original object. When assigning a new object to obj inside the IIFE, it doesn’t affect the original obj outside the IIFE.Question: What will be the output of the following code? Why?
var length = 10;
function fn() {
console.log(this.length);
}
var obj = {
length: 5,
method: function(fn) {
fn();
arguments[0]();
}
};
obj.method(fn, 1);
Answer:
102fn() call is executed with the global context (this refers to the global object where length is 10).arguments[0]() call invokes fn with arguments as the context, where arguments.length is 2 because arguments contains two items.These questions cover different aspects of JavaScript, from scoping and closures to quirks in type coercion and function context. Mastering these will definitely prepare you well for any JavaScript interview!
Not yet included in a published release.