JavaScript Ques
JavaScript Ques
● Dynamic typing
● Object-oriented programming (prototypal inheritance)
● First-class functions
● Event-driven programming
● Asynchronous programming (using promises, async/await)
● Lightweight and cross-platform.
js
Copy code
(function() {
console.log('IIFE');
})();
10. What is the difference between synchronous and asynchronous code in JavaScript?
15. Explain arrow functions in JavaScript and how they differ from regular functions.
Arrow functions are a concise syntax for writing functions. They do not have their own this
context and inherit this from the surrounding lexical context.
js
Copy code
const add = (a, b) => a + b;
16. What is the purpose of the bind(), call(), and apply() methods?
These methods are used to set the value of this in functions:
js
Copy code
const name = "John";
console.log(`Hello, ${name}!`);
20. What is the difference between map(), forEach(), filter(), and reduce()
methods in arrays?
● map() creates a new array with the results of applying a function to every element.
● forEach() executes a function for each array element, without returning a new array.
● filter() creates a new array with elements that pass a condition.
● reduce() reduces an array to a single value by applying a function cumulatively.
js
Copy code
const arr = [1, 2, 3];
console.log(...arr); // 1 2 3
● A shallow copy copies the top-level properties but nested objects share references.
● A deep copy creates a complete copy of an object, including nested objects.
js
Copy code
const [a, b] = [1, 2];
const { name } = { name: "John", age: 30 };
25. What are JavaScript modules, and how do you export and import them?
JavaScript modules allow you to split your code into reusable files. You can use export to
make variables, functions, or classes available outside a module, and import to bring them into
another file.
js
Copy code
export const hello = () => console.log("Hello");
import { hello } from './module';
27. How does JavaScript handle memory management and garbage collection?
JavaScript has an automatic garbage collector that identifies and frees memory occupied by
objects no longer in use, typically through reference counting and mark-and-sweep algorithms.
28. What are events in JavaScript, and how do you handle them?
Events are actions or occurrences detected by JavaScript (e.g., user clicks, form submission).
You can handle them using event listeners.
js
Copy code
document.addEventListener('click', () => console.log('Clicked!'));
js
Copy code
function Person(name) {
this.name = name;
}
const john = new Person("John");
js
Copy code
const add = a => b => a + b;
console.log(add(2)(3)); // 5
31. What is the event loop in JavaScript, and how does it work?
The event loop continuously checks the call stack and callback queue, executing functions from
the queue only when the stack is empty, enabling asynchronous behavior.
32. What are arrow functions, and how do they differ from traditional functions?
Arrow functions have a shorter syntax, and unlike traditional functions, they don't have their own
this context, which is lexically bound.
33. What are JavaScript objects, and how do you create them?
JavaScript objects are collections of key-value pairs. They can be created using object literals or
constructors.
js
Copy code
const person = { name: "John", age: 30 };
36. What is the difference between the find() and filter() methods in arrays?
js
Copy code
const person = {
firstName: "John",
get fullName() {
return this.firstName + " Doe";
},
set firstName(name) {
this.firstName = name;
}
};
38. What is NaN, and how can you check if a value is NaN?
NaN stands for "Not-a-Number". You can check if a value is NaN using the isNaN() function or
Number.isNaN().
39. What is the difference between let and var in terms of scope and hoisting?
● slice() returns a shallow copy of a portion of an array without modifying the original.
● splice() modifies the original array by removing, replacing, or adding elements.
js
Copy code
function greet(name = 'Guest') {
console.log(`Hello, ${name}`);
}
● Event bubbling: The event starts at the target element and bubbles up to the ancestors.
● Event capturing: The event starts from the document and captures down to the target
element.
49. What is the difference between arrow functions and regular functions in terms of
this binding?
Arrow functions do not have their own this. They inherit this from the surrounding context.
Regular functions have their own this based on how they are called.
50. How do JavaScript promises prevent "callback hell"?
Promises flatten the nesting of callbacks by chaining .then() calls, making the code more
readable and easier to follow, compared to deeply nested callbacks.