0% found this document useful (0 votes)
162 views

JS - Interview

The document provides an overview of key JavaScript concepts, including variables, functions, closures, and asynchronous programming with Promises and async/await. It explains the differences between data types, variable declarations (var, let, const), and comparison operators (== vs ===). Additionally, it covers event delegation, the this keyword, function declarations vs expressions, and other important features like destructuring, template literals, and the spread operator.

Uploaded by

mrpubg632
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
162 views

JS - Interview

The document provides an overview of key JavaScript concepts, including variables, functions, closures, and asynchronous programming with Promises and async/await. It explains the differences between data types, variable declarations (var, let, const), and comparison operators (== vs ===). Additionally, it covers event delegation, the this keyword, function declarations vs expressions, and other important features like destructuring, template literals, and the spread operator.

Uploaded by

mrpubg632
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

● Variables – Store data using var, let, and const.

● Functions – Reusable blocks of code that perform specific tasks.

● Hoisting – JavaScript moves declarations to the top before execution.

● Closures – A function inside another function that remembers outer variables.

● Promises & Async/Await – Handle asynchronous operations efficiently.

● DOM Manipulation – Interact with and update HTML elements dynamically.

● Event Handling – Respond to user actions like clicks and keypresses.

● Prototype & Inheritance – Helps in object-oriented programming in JavaScript.

Ques 1: What is JavaScript? Why do we need it?


JavaScript is a high-level, interpreted programming language primarily used to create
interactive and dynamic content on web pages.

● It enables developers to implement complex features such as real-time updates,


interactive forms, animations, and more.

● 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.

Ques 2: What are the different data types in JavaScript?

Ans.

JavaScript supports several data types, categorized into:

Primitive Types: These include:

● String: Represents textual data. Example: “Hello, World!”


Number: Represents numerical values. Example: 42 or 3.14

● Boolean: Represents logical values. Example: true or false

● 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.

● BigInt: Represents integers with arbitrary precision. Example:


1234567890123456789012345678901234567890n

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.

Ques 3: What is the difference between var, let, and const?

In JavaScript, variables can be declared using var, let, or const, each with distinct
characteristics:

var:

● Scope: Function-scoped. If declared outside a function, it becomes globally scoped.

● 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:

● Scope: Block-scoped, similar to let.

● Hoisting: Behaves like let with hoisting.


● Re-declaration and Update: Cannot be re-declared or updated within the same scope.
However, for objects, while the variable reference cannot be changed, the object’s
properties can be modified.

Ques 4: What is a closure in JavaScript?

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.

● Closures are often used to create private variables or functions.

Ques 5: Explain event delegation in JavaScript.

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.

In JavaScript, == and === are comparison operators with distinct behaviors:

== (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.

=== (Strict Equality):

● Compares both the value and the type without performing type conversion.

● Example: 5 === ‘5’ returns false because the types (number and string) are different.

Ques 7: What is the purpose of the this keyword in JavaScript?

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.

● Constructor Context: In a constructor function, this refers to the newly created


instance.

● 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.

In JavaScript, functions can be defined in two primary ways:

● Function Declaration: A function is declared using the function keyword and can be
called before its definition due to hoisting.

Function Expression: A function is assigned to a variable. It is not hoisted, so it cannot be called


before the definition.

● 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.

Ques 10: What is an Immediately Invoked Function Expression (IIFE)?

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.

Ques 12: What are template literals in JavaScript?

Ans.

Template literals (introduced in ES6) are enclosed in backticks (` `) and allow:


● Multiline strings

● String interpolation

● Embedded expressions

Ques 13: What is the difference between map(), filter(), and reduce()?

Ans.

These are array methods that manipulate elements:

● map(): Creates a new array by applying a function to each element.

● filter(): Returns a new array with elements that pass a test.

● reduce(): Reduces an array to a single value.

Ques 14: What is destructuring in JavaScript?

Ans.

Destructuring is a feature that allows extracting values from arrays or objects into variables.

Array destructuring:
Object destructuring:

Ques 16: What are Promises in JavaScript?

Ans.

A Promise in JavaScript is an object that represents the eventual result (or failure) of an
asynchronous operation.

States of a Promise:

● Pending: Initial state, before the operation completes.

● Fulfilled: The operation was successful.

● Rejected: The operation failed.

Ques 17: What is async/await in JavaScript?

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:

● Used to declare a function that always returns a Promise.

● Automatically wraps the return value in a Promise.

await keyword:

● Can only be used inside async functions.


● Pauses the function execution until the Promise is resolved.

● Makes the code look synchronous.

Ques 19: What is the typeof operator?

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.

Ques 20: What is the spread operator in JavaScript?

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.

You might also like