0% found this document useful (0 votes)
2 views3 pages

JavaScript Practice Questions

The document consists of a series of JavaScript practice questions covering various topics such as variables and scope, data types and conversions, functions, objects and arrays, the 'this' keyword, callbacks, promises, async/await, and the event loop. Each section includes specific questions that require explanations, code examples, and predictions of outputs. The questions are designed to test understanding of fundamental JavaScript concepts and their practical applications.

Uploaded by

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

JavaScript Practice Questions

The document consists of a series of JavaScript practice questions covering various topics such as variables and scope, data types and conversions, functions, objects and arrays, the 'this' keyword, callbacks, promises, async/await, and the event loop. Each section includes specific questions that require explanations, code examples, and predictions of outputs. The questions are designed to test understanding of fundamental JavaScript concepts and their practical applications.

Uploaded by

selciasylin04
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

JavaScript Practice Questions

Variables & Scope (var, let, const, block vs function scope)


1. What will be the output of the following?

var x = 1;
{
let x = 2;
[Link](x);
}
[Link](x);

2. Explain the difference in scoping between var and let. Write a small code to show it.

3. Why does const not make objects immutable? Show an example.

4. Write a function using var that demonstrates hoisting.

5. What happens if you re-declare a var variable inside the same scope vs re-declaring a let
variable? Try with code.

Data Types & Conversions (primitive vs non-primitive, == vs ===, type coercion)


1. What will this return? Why?

[Link]([] == 0);
[Link]([] === 0);

2. Show with an example: how primitives are stored by value and objects by reference.

3. Convert '123' to a number in 3 different ways.

4. Why does null == undefined return true but null === undefined return false?

5. Write code that demonstrates type coercion in arithmetic operations.

Functions (declaration vs expression, IIFE, default parameters, rest & spread)


1. Difference between function declaration and function expression with example.

2. Write an IIFE that logs 'Hello from IIFE'.

3. Write a function with a default parameter and call it without arguments.

4. Write a function that takes multiple arguments using rest parameters and returns their
sum.
5. Copy an array using spread operator and show that modifying one doesn’t affect the
other.

Objects & Arrays (destructuring, shallow vs deep copy)


1. Destructure the object {name: 'Priya', age: 22, city: 'Ooty'}.

2. Destructure an array const arr = [10, 20, 30].

3. Show shallow copy with [Link]() and explain why changing nested objects affects
both copies.

4. Create a deep copy using [Link]([Link]()). Show the difference from shallow.

5. Use destructuring with default values.

This keyword (global, function, arrow function, object methods)


1. What is 'this' in global scope in [Link] vs Browser?

2. Write a function inside an object that uses 'this' to access its property.

3. Show the difference between 'this' in normal function vs arrow function.

4. Why does 'this' behave differently inside setTimeout? Demonstrate.

5. Use call, apply, and bind to explicitly set 'this'.

Callbacks, Promises & Async/Await


1. Write a function that takes a callback and calls it after 2 seconds.

2. Convert a callback function into a Promise-based version.

3. Use [Link] to wait for 3 promises and log results together.

4. Use [Link] to log whichever promise resolves first.

5. Write an async function with await that fetches from a fake API
([Link]

Event Loop & Execution Context


1. Predict output:

[Link]('A');
setTimeout(() => [Link]('B'), 0);
[Link]('C');

2. Explain the difference between call stack and task queue with an example.
3. Predict output:

[Link]().then(() => [Link]('Promise'));


[Link]('Sync');

4. Why does async code not block the main thread? Show with setTimeout.

5. Trace execution context for a function that calls another function inside it.

You might also like