JavaScript
Q
What are the possible ways to create objects in JavaScript?
Object literals ({}), constructor functions (new Object()),
Object.create(), ES6 classes (class), and the
new keyword with a custom function. Each method provides different control
over prototype chains and property definitions.
JavaScript
Q
What is the difference between == and === operators?
== performs type coercion before comparison, so "5" == 5 is true.
=== checks both value and type without coercion, so "5" === 5 is false.
Always prefer === for predictable results.
JavaScript
Q
What are arrow functions?
Arrow functions are a concise syntax introduced in ES6. They do not have their own
this, arguments, super, or new.target.
They are great for short callbacks and functional programming patterns.
JavaScript
Q
What is a higher order function?
A higher‑order function either takes one or more functions as arguments, returns a function,
or both. Examples: Array.map(), Array.filter(), and custom
function wrappers.
JavaScript
Q
What is a pure function?
A pure function always returns the same output for the same input and has no side effects
(does not modify external state). It is the foundation of functional programming and
simplifies testing and reasoning about code.
JavaScript
Q
What is the difference between let, var and const?
var is function‑scoped and hoisted, let is block‑scoped,
and const is block‑scoped and cannot be reassigned (though objects can
be mutated). let and const do not hoist in the same way as
var; they exist in the Temporal Dead Zone.
JavaScript
Q
What is the Temporal Dead Zone?
The Temporal Dead Zone (TDZ) is the period between entering a scope and the actual
declaration of a let or const variable. Accessing the variable
in this zone results in a ReferenceError.
JavaScript
Q
What is Hoisting?
Hoisting is JavaScript’s default behavior of moving declarations to the top of their scope
during compilation. var variables are hoisted and initialized with
undefined, while let and const are hoisted but
remain in the TDZ. Function declarations are fully hoisted.
JavaScript
Q
What are closures?
A closure is created when a function retains access to variables from its outer (enclosing)
scope even after that outer function has finished executing. Closures are essential for
data privacy, currying, and module patterns.
JavaScript
Q
What is scope in JavaScript?
Scope determines the accessibility of variables. JavaScript has three main scopes:
global, function (local), and block (with let/const).
Lexical scoping means inner functions can access outer variables, forming closures.
JavaScript
Q
What are the differences between cookie, local storage and session storage?
- Cookies: ~4KB, sent with every HTTP request, can have expiration dates.
- Local Storage: ~5‑10MB, persists until explicitly cleared, never sent to server.
- Session Storage: ~5‑10MB, cleared when the tab/browser is closed, also client‑side only.
JavaScript
Q
What are the three states of promise?
Pending (initial state), Fulfilled (operation completed
successfully), and Rejected (operation failed). Once settled, a promise
cannot change state.
JavaScript
Q
What is a callback function?
A callback is a function passed as an argument to another function and executed later,
often after an asynchronous operation completes. They are the traditional way to handle
async code, now often replaced by promises or async/await for clarity.
JavaScript
Q
What are template literals?
Template literals (delimited by backticks `) allow embedded expressions
(${expression}), multi‑line strings, and tagged templates. They provide a
cleaner way to build strings compared to concatenation.
JavaScript
Q
What is a strict mode in JavaScript?
"use strict" enables a stricter set of rules that catch common coding mistakes
and “unsafe” actions. It prevents accidental globals, eliminates silent errors, and makes
the code more secure and optimizable.
JavaScript
Q
What is null value?
null represents the intentional absence of any object value. It is one of
JavaScript’s primitive values and is often used to indicate “no value” or “empty”.
JavaScript
Q
What are events?
Events are actions or occurrences that happen in the browser (e.g., clicks, key presses,
page load) that JavaScript can listen for and respond to using event handlers.
JavaScript
Q
What is the use of the preventDefault method?
event.preventDefault() stops the browser’s default behavior for that event,
such as preventing a link from navigating or a form from submitting.
JavaScript
Q
What is the use of the stopPropagation method?
event.stopPropagation() prevents the event from bubbling up or capturing
down the DOM tree, stopping parent handlers from being notified of the event.
JavaScript
Q
What is the use of setTimeout?
setTimeout() executes a function once after a specified delay (in
milliseconds). It is fundamental for scheduling delayed actions or simulating
asynchronous behavior.
JavaScript
Q
What is the use of setInterval?
setInterval() repeatedly calls a function with a fixed time delay between
each call. It is used for periodic tasks like updating a clock or polling a server.
JavaScript
Q
What is the purpose of JSON.stringify?
JSON.stringify() converts a JavaScript object or value to a JSON string.
It is commonly used to send data to a server or store it in a text‑based format.
JavaScript
Q
How do you redirect to a new page in JavaScript?
window.location.href = "https://example.com" or
window.location.replace("https://example.com") (the latter replaces the
current history entry, preventing back‑button return).
JavaScript
Q
What are the string methods available in Regular expression?
match(), search(), replace(), split(),
and matchAll() all accept regular expressions to perform pattern‑based
operations on strings.
JavaScript
Q
Syntax of conditional (ternary) operator in JavaScript.
condition ? expressionIfTrue : expressionIfFalse. It is a concise
alternative to an if…else statement when you need to return a value.
JavaScript
Q
Example for rest parameter.
function sum(...numbers) { return numbers.reduce((a,b) => a+b, 0); }
The rest parameter (...numbers) collects all remaining arguments into an
array, providing a clean alternative to the arguments object.
JavaScript
Q
Combine two arrays using a spread operator.
const combined = [...array1, ...array2]. The spread operator expands
each array’s elements into a new array, offering a concise way to merge arrays.
JavaScript
Q
Explain Implicit Type Conversion in JavaScript.
JavaScript automatically converts values from one type to another when an operator is
used with mismatched types (e.g., "5" + 2 → "52", "5" - 2 → 3).
This behavior is called type coercion and can lead to unexpected results if not understood.
JavaScript
Q
Is JavaScript a statically typed or a dynamically typed language?
JavaScript is a dynamically typed language. Variable types are determined
at runtime and can change during execution. TypeScript adds static typing on top of
JavaScript.
JavaScript
Q
Explain the “this” keyword.
this refers to the execution context. In a method, it points to the object
that owns the method; in a regular function call, it points to the global object (or
undefined in strict mode); arrow functions inherit this from
their surrounding scope.
JavaScript
Q
What is the difference between DOM and BOM?
DOM (Document Object Model) represents the HTML document structure,
allowing manipulation of elements. BOM (Browser Object Model) provides
objects that interact with the browser outside the document, like window,
navigator, history, and location.
JavaScript
Q
List DOM methods to access HTML element references.
getElementById(), getElementsByClassName(),
getElementsByTagName(), querySelector(), and
querySelectorAll(). They return live or static collections of elements.
JavaScript
Q
Difference between for…in & for..of
for…in iterates over enumerable property keys (including
inherited ones). for…of iterates over values of an
iterable object (arrays, strings, maps, etc.), following the iteration protocol.
JavaScript
Q
List a few ES6+ features.
Arrow functions, classes, template literals, destructuring assignment, default parameters,
rest/spread operators, let and const, promises, modules,
and Symbol are among the most impactful.
JavaScript
Q
Difference between object & JSON.
A JavaScript object is a data structure with properties and methods, existing in memory.
JSON (JavaScript Object Notation) is a text‑based data‑interchange format. JSON is a
string; it can be parsed into an object using JSON.parse().
JavaScript
Q
Difference between innerHTML & innerText.
innerHTML returns or sets the HTML content of an element, parsing any tags.
innerText returns only the visible text content, ignoring hidden elements
and not parsing HTML, and it respects CSS styling.
JavaScript
Q
Difference between slice & splice methods.
slice(start, end) returns a shallow copy of a portion of an array without
modifying the original. splice(start, deleteCount, ...items) changes the
original array by removing/replacing elements and optionally inserting new ones.
JavaScript
Q
Differentiate between map() and filter() with syntax.
array.map(callback) creates a new array by applying a function to every
element. array.filter(callback) creates a new array containing only elements
that pass a test. map transforms each item; filter selects
items.
JavaScript
Q
How do you validate email in JavaScript?
Use a regular expression like
/^[^\s@]+@[^\s@]+\.[^\s@]+$/ and test the email string.
For more robust validation, rely on built‑in HTML5 input type email or
backend verification, as regex alone cannot guarantee deliverability.
JavaScript
Q
What is the difference between exec() and test() methods in JavaScript?
test() returns a boolean indicating whether the pattern matches.
exec() returns an array with detailed match information (or
null if no match). exec() also updates the regex’s
lastIndex when using the global flag.