JS - Interview
JS - Interview
● Unlike HTML and CSS, which structure and style web content respectively, JavaScript
adds behavior to web pages, allowing for user engagement and dynamic content
updates without requiring a page reload.
Ans.
● Null: Represents the intentional absence of any object value. Example: null
● Undefined: Indicates a variable that has been declared but not assigned a value.
Example: undefined
● Symbol: Represents a unique and immutable value, often used as object property
identifiers.
Non-Primitive Types:
● Object: A collection of key-value pairs. Objects can represent more complex data
structures like arrays, functions, dates, and regular expressions.
Understanding these data types is crucial for effective variable management and function
operations in JavaScript.
In JavaScript, variables can be declared using var, let, or const, each with distinct
characteristics:
var:
● Hoisting: Declarations are hoisted to the top of their scope and initialized with
undefined.
● Re-declaration and Update: Can be re-declared and updated within its scope.
let:
● Scope: Block-scoped, confined to the block (enclosed by {}) where it’s declared.
● Hoisting: Declarations are hoisted but not initialized, leading to a “temporal dead zone”
until the declaration is encountered.
● Re-declaration and Update: Cannot be re-declared within the same scope but can be
updated.
const:
Ans.
A closure is a function that retains access to its lexical scope, even when the function is
executed outside that scope.
● This means the inner function has access to variables and parameters of its outer
function, even after the outer function has finished executing.
Event delegation is a technique where a single event listener is added to a parent element to
manage events for its child elements.
● Instead of attaching individual event listeners to each child element, the parent element
listens for events, and through event propagation (bubbling), it can capture events from
its descendants.
● This approach enhances performance and simplifies code management, especially when
dealing with dynamic content.
● In this example, a click event listener is added to the parent element with the ID parent.
When any button with the class className inside this parent is clicked, the event is
captured by the parent, and the specified action is executed.
Ques 6: What is the difference between == and === in JavaScript? (Most important Question)
Ans.
== (Loose Equality):
● Compares two values for equality after converting both values to a common type (type
coercion).
● Example: 5 == ‘5’ returns true because the string ‘5’ is converted to the number 5
before comparison.
● Compares both the value and the type without performing type conversion.
● Example: 5 === ‘5’ returns false because the types (number and string) are different.
Ans.
The this keyword refers to the object from which a function was called. Its value depends on
the context in which the function is executed:
● Global Context: In the global scope, this refers to the global object (window in
browsers).
● Function Context: Inside a regular function, this refers to the global object in non-strict
mode and undefined in strict mode.
● Method Context: When a function is called as a method of an object, this refers to the
object itself.
● In this example, within the greet method, the this keyword refers to obj, so this.name
returns ‘Alice’.
Ques 8: What is the difference between function declaration and function expression?
Ans.
● Function Declaration: A function is declared using the function keyword and can be
called before its definition due to hoisting.
● Function expressions are commonly used when assigning functions to variables, passing
them as arguments, or using them in closures.
Ques 9: What is Arrow Functions in JavaScript?
Ans.
Ans.
An IIFE (Immediately Invoked Function Expression) is a JavaScript function that runs as soon as
it is defined. It prevents polluting the global scope by encapsulating variables inside the
function.
Ans.
● String interpolation
● Embedded expressions
Ques 13: What is the difference between map(), filter(), and reduce()?
Ans.
Ans.
Destructuring is a feature that allows extracting values from arrays or objects into variables.
Array destructuring:
Object destructuring:
Ans.
A Promise in JavaScript is an object that represents the eventual result (or failure) of an
asynchronous operation.
States of a Promise:
async/await is a modern way to work with Promises in JavaScript, making asynchronous code
look and behave like synchronous code (easier to read and manage).
async keyword:
await keyword:
Ans.
The typeof operator is used to determine the type of a given operand. It returns a string
indicating the type of the operand, such as number, string, boolean, etc.
Ans.
The spread operator (…) allows you to unpack elements from an array or object and spread
them into a new array or object. It is often used for copying or merging arrays/objects.
Ques 22: Explain the concept of hoisting in JavaScript.
Ans.
Hoisting is a behavior in JavaScript where variable and function declarations are moved to the
top of their containing scope during the compile phase. However, only the declarations are
hoisted, not the assignments.
● Variables: Only the declaration (var x;) is hoisted, not the assignment (x = 10;).
● Functions: Both function declarations and their definitions are hoisted, but function
expressions are not.