Basic JavaScript Interview Questions (Freshers)
1. What are the different data types in JavaScript?
JavaScript has primitive data types: String, Number, BigInt, Boolean, Undefined, Null, Symbol. It also has a non-primitive type: Object (including arrays, functions, dates, etc.).
[2][4]
2. What is the difference between == and ===?
== performs type coercion before comparison, while === compares both value and type without coercion.
console.log(5 == '5'); // true
console.log(5 === '5'); // false
[2]
3. What is hoisting in JavaScript?
Hoisting moves variable and function declarations to the top of their scope before code execution. Only declarations are hoisted, not initializations.
[4]
4. What are truthy and falsy values in JavaScript?
Falsy values are false, 0, 0n, -0, ”, null, undefined, NaN. All other values are truthy.
[4]
5. What is the result of 3 + 2 + "7"?
The result is "57" because numbers are added first (3+2=5), then concatenated with the string “7”.
[2]
6. Explain the difference between var, let, and const.
var has function scope and can be redeclared. let and const have block scope. let can be reassigned, const cannot.
[2]
var x = 10;
let y = 20;
const z = 30;
// y = 40; // allowed
// z = 50; // error
7. What is strict mode in JavaScript?
Strict mode (‘use strict’) enforces stricter parsing and error handling, catching common coding errors and preventing unsafe actions.
[4]
8. How do you create an object in JavaScript?
Objects can be created using object literals, Object.create(), new Object(), constructor functions, or ES6 classes.
[5]
const person = {name: 'John', age: 30};
const person2 = Object.create({greet() {}});
const Person = class {constructor(name) {this.name = name;}};
const john = new Person('John');
9. What does this refer to in different contexts?
In global scope, this is the window object. In object methods, it refers to the object. In regular functions, it depends on how the function is called.
[2]
const obj = {
name: "JS",
say() { console.log(this.name); // "JS"
}
};
obj.say();
10. What is the output of console.log(typeof null)?
The output is "object". This is a historical bug in JavaScript where null is considered an object.
[2]
Intermediate JavaScript Interview Questions (1-3 Years Experience)
11. What are closures in JavaScript?
A closure is a function that retains access to its outer scope’s variables even after the outer function has returned.
function outer() {
let count = 0;
return function inner() {
count++;
return count;
};
}
const counter = outer();
console.log(counter()); // 1
console.log(counter()); // 2
12. Explain event bubbling and capturing.
Event bubbling: events propagate from target to root. Event capturing: events propagate from root to target. Default is bubbling.
[2]
13. What is the difference between null and undefined?
undefined means a variable has been declared but not assigned. null is an intentional absence of value.
[2]
14. How does call(), apply(), and bind() differ?
All change this context. call() and apply() invoke immediately; apply() takes array arguments. bind() returns a new function.
function greet(greeting) {
console.log(greeting + ' ' + this.name);
}
const person = {name: 'John'};
greet.call(person, 'Hi'); // Hi John
greet.apply(person, ['Hello']); // Hello John
const bound = greet.bind(person, 'Hey'); // returns function
15. What is prototypal inheritance?
JavaScript objects inherit properties from other objects via the prototype chain. Each object has a __proto__ linking to its prototype.
[5]
16. Explain Object.keys(), Object.values(), and Object.entries().
Object.keys() returns property names array. Object.values() returns values array. Object.entries() returns [key, value] pairs array.
[5]
const obj = {a: 1, b: 2};
console.log(Object.keys(obj)); // ['a', 'b']
console.log(Object.values(obj)); // [1, 2]
console.log(Object.entries(obj)); // [['a',1], ['b',2]]
17. What are arrow functions and their limitations?
Arrow functions provide concise syntax and lexical this. They cannot be used as constructors and lack their own this, arguments.
const add = (a, b) => a + b;
const obj = {
name: 'JS',
regular: function() { return this.name; },
arrow: () => this.name // undefined
};
18. How do you merge two objects in JavaScript?
Use the spread operator ... or Object.assign().
const obj1 = {a: 1};
const obj2 = {b: 2};
const merged = {...obj1, ...obj2}; // {a:1, b:2}
const merged2 = Object.assign({}, obj1, obj2);
Advanced JavaScript Interview Questions (3-6 Years Experience)
19. What is the event loop in JavaScript?
The event loop handles asynchronous callbacks. It continuously checks the call stack and task queue, moving tasks from queue to stack when stack is empty.
[2]
20. Explain Promises and their states.
Promises represent eventual completion/failure. States: pending, fulfilled, rejected. Use .then(), .catch(), .finally().
[2]
const promise = new Promise((resolve, reject) => {
setTimeout(() => resolve('Success'), 1000);
});
promise.then(result => console.log(result));
21. What is async/await and how does it work?
async functions return Promises. await pauses execution until Promise settles. Better error handling with try/catch.
[2]
async function fetchData() {
try {
const data = await fetch('url');
return data.json();
} catch (error) {
console.error(error);
}
}
22. What is debouncing in JavaScript?
Debouncing delays function execution until after a wait period from last invocation. Useful for search inputs, scroll events.
[5]
function debounce(fn, delay) {
let timeout;
return function(...args) {
clearTimeout(timeout);
timeout = setTimeout(() => fn.apply(this, args), delay);
};
}
23. How would you implement error handling in a Salesforce JavaScript application?
Use try-catch blocks, window.onerror for uncaught errors, and Promise rejection handling. Log errors with context for debugging.
[2][3]
24. Explain Promise.all() and Promise.race().
Promise.all() resolves when all promises resolve, rejects on first rejection. Promise.race() resolves/rejects with fastest promise.
Promise.all([p1, p2]).then(results => console.log(results));
Promise.race([p1, p2]).then(result => console.log(result));
25. What are generators in JavaScript?
Generators are functions that pause/resume execution using yield. Return iterator objects for custom iteration.
function* generator() {
yield 1;
yield 2;
return 3;
}
const gen = generator();
console.log(gen.next()); // {value: 1, done: false}
26. How do you handle memory leaks in JavaScript?
Avoid circular references, remove event listeners, use weak references (WeakMap/WeakSet), and clear timers/intervals properly.
27. What is the temporal dead zone (TDZ)?
TDZ is the period between entering a block scope and variable initialization where accessing let/const variables throws ReferenceError.
[2]
28. Explain Proxy and Reflect in JavaScript.
Proxy creates object intercepting operations. Reflect provides methods for interceptable operations, used in Proxy handlers.
const target = {prop: 1};
const handler = {
get(target, prop) {
return Reflect.get(...arguments);
}
};
const proxy = new Proxy(target, handler);
29. How would you optimize rendering performance at Atlassian using JavaScript?
Use requestAnimationFrame for animations, virtual scrolling for large lists, memoization, and avoid unnecessary DOM manipulations.
30. What strategies would you use to handle large-scale state management in a Zoho JavaScript application?
Implement centralized state with immutable updates, use Proxy for reactivity, event emitters for communication, and modular reducers for scalability.