JavaScript : Acing the Interview - The Ultimate Q&A Guide (Part 11)
JavaScript 101: Acing the Interview - The Ultimate Q&A Guide (Part 11)
Acing Your Answers: The Structure for Success
Direct Definition: Start with a clear, concise definition. Key Differentiators: Explain the crucial differences or behaviors. Real-World Analogy/Example: Use a simple analogy or a code snippet to prove you understand the practical application. Best Practice/Conclusion: Briefly state the modern best practice or why the concept is important.
The Q&A Gauntlet
Q1: What are the different data types in JS?
String: For text, like "hello". Number: For all numeric values, like 42 or 3.14. Boolean: For logical values, true or false. : The default value of a declared but uninitialized variable. : Represents the intentional absence of a value. Symbol and BigInt are newer primitives for unique identifiers and very large integers.
Q2: What is the difference between == and === in JS?
Strict Equality ( compares two values for equality of both value and type. It does not perform any type conversion. Loose Equality ( will first try to coerce (convert) the values to a common type before comparing their values.
5 === "5" returns false because the types (Number and String) are different.5 == "5" returns true because JavaScript converts the string "5" to a number before comparing. This can lead to unexpected bugs.
Q3: Explain the difference between null and undefined.
undefined is what JavaScript assigns to a variable that has been declared but has not been given a value. It's the default "empty" state.null is a value that a programmer explicitly assigns to a variable to signify "no value" or to deliberately clear a variable's contents.
Q4: What are the differences between var, let, and const?
Scope: var is function-scoped, meaning it's available anywhere in the function it's defined in. let and const are block-scoped, meaning they only exist within the curly braces {} they are defined in. Hoisting: var declarations are hoisted and initialized with undefined. let and const are hoisted but not initialized, creating a "Temporal Dead Zone" where they cannot be accessed before their declaration line. Re-assignment: var and let can be reassigned. const cannot be reassigned after its initial declaration, making it a constant reference.
Q5: Explain Hoisting in JS.
Hoisting: Only the declaration (var myVar;) is hoisted, not the assignment (= 10). This is why accessing it before the assignment line yields undefined. Function Declaration Hoisting: The entire function body (function myFunc() {...}) is hoisted, which allows you to call it before it appears in the code. , These are hoisted but not initialized, so trying to access them before their declaration line results in a ReferenceError.
Q6: What are Closures in JS?
Even though the createCounter function has finished running, counterA and counterB each remember their own private count variable. This is a powerful pattern for creating functions with private, persistent state."
Q7: Differentiate between map(), filter(), and reduce().
(The Transformer): It creates a new array of the exact same length as the original. It does this by applying a callback function to each element and collecting the results. Use it when you want to transform every item in a list. (The Gatekeeper): It creates a new array that contains only the elements from the original that pass a test. The callback function must return true to keep an element. The new array can be the same length or shorter. (The Accumulator): It iterates through an array to produce a single, final value. It takes a callback with an accumulator and the currentItem, effectively "reducing" the whole array into one value (like a sum, a string, or an object).
.map(p => p.name) gives you a new array of just the product names. .filter(p => p.price < 50) gives you a new, shorter array of only the cheap products. .reduce((total, p) => total + p.price, 0) gives you a single number: the total price of all products."
Q8: What is a callback function in JavaScript?
Q9: Explain array destructuring and object destructuring in JavaScript.
Array Destructuring: Unpacks values based on their position (index) in the array. Object Destructuring: Unpacks properties based on their key name.
Q10: Describe the features described in ES6.
and For block-scoped variable declarations, which are safer than var. Arrow Functions ( A more concise syntax for writing functions. Destructuring: For easily unpacking values from arrays and objects. Template Literals: For cleaner multi-line strings and string interpolation using backticks and ${}. Spread ( For expanding elements and gathering them into arrays. Promises: A native way to handle asynchronous operations more cleanly than callbacks. Classes: A simpler syntax for creating constructor functions and managing prototypes."
Q11: What are the differences between arrow functions and function expressions?
Syntax: Arrow functions are shorter. (a, b) => a + b is much more concise than function(a, b) { return a + b; }. They allow for an 'implicit return' if there are no curly braces. Binding: This is the critical difference. A regular function gets its own this context, determined by how it is called. An arrow function does not have its own this; it lexically inherits this from its parent scope. object: Regular functions have access to a special arguments object that contains all the arguments passed to it. Arrow functions do not.
Q12: How are arrays different from objects in JavaScript?
Structure & Keys: Arrays use ordered, integer-based indices as keys (0, 1, 2, etc.). Objects use unordered, custom string keys (e.g., 'name', 'age'). Purpose: Arrays are ideal for lists of items where the order matters (e.g., a to-do list, a user queue). Objects are ideal for describing the properties of a single entity where order doesn't matter (e.g., a user profile, product details). Methods: Arrays come with a rich set of built-in methods for iteration and manipulation, like .map(), .filter(), and .push(), which objects do not have.
Q13: Explain the difference between dot notation and square bracket notation when accessing object properties. When would you use one over the other?
Dot Notation ( This is the shorthand. It's clean and easy to read, but the property name must be a valid JavaScript identifier (no spaces, not starting with a number, etc.). Bracket Notation ( This is more versatile. The key inside the brackets is evaluated as a string. This means you can: Access properties with invalid identifier names (e.g., 'first-name' or '2nd-place'). Use a variable to dynamically determine which property to access.
Comments
Post a Comment