Javascript Interview Questions
1.Q: What is JavaScript, and how is it different from HTML and CSS?
A: JavaScript is a scripting language used to add interactivity and logic to web pages. While
HTML structures content and CSS styles it, JavaScript controls behavior like clicks, animations,
or dynamic updates.
2. Q: What is the difference between function scope and block scope in JavaScript?
A: Variables declared with var are function-scoped, meaning they exist throughout the function.
Variables declared with let or const are block-scoped and only exist within the {} block they’re
declared in.
3. Q: What is asynchronous programming, and why is it useful in JavaScript?
A: Asynchronous programming lets tasks like data fetching or timers run in the background
without blocking the main thread. It keeps the webpage responsive while waiting for long-
running tasks.
4. Q: What is AJAX, and how does it help in building dynamic websites?
A: AJAX (Asynchronous JavaScript and XML) allows JavaScript to request data from a server in
the background and update parts of a webpage without reloading the whole page.
5. Q: How is an array different from an object in JavaScript?
A: An array is a special type of object used to store ordered collections of values, accessed by
index. Objects store key–value pairs and are unordered by default.
6. Q: What is the difference between == and === in JavaScript?
A: == checks for value equality with type conversion (loose equality), while === checks for both
value and type equality (strict equality), making === safer for comparisons.
7. Q: How do you select and modify an element’s content using the DOM?
A: Use document.getElementById("id") or querySelector() to select the element, then modify it
using properties like innerText or textContent.
8. Q: How does .push() work on arrays?
A: The .push() method adds one or more elements to the end of an array and returns the new
length of the array.
9. Q: What is a callback function in JavaScript?
A: A callback is a function passed into another function to be executed later, often used in
asynchronous operations like setTimeout() or event handling.
10. Q: What is undefined in JavaScript, and how is it different from null?
A: undefined means a variable has been declared but not assigned a value. null is an
intentional absence of value set by the programmer.
11.Q: You want to declare a variable to store a user's name that won’t change. How would you
do that?
Answer: I’d use const, like const userName = "Alice";, since the value won't change. It ensures
the variable is block-scoped and cannot be reassigned.
12. Q: You want to log "Done!" after waiting 2 seconds. What built-in async function helps?
Answer: I’d use setTimeout. It lets me delay a piece of code—for example, printing “Done!”
after 2 seconds—without blocking the rest of the script.
13. Q: How would you fetch data from an API like https://api.example.com/users and log the
result?
Answer: I’d use the fetch function. It returns a promise, so I’d handle the response using .then()
to convert it to JSON and then log the data once it’s received.
14. Q: You want to get all numbers greater than 10 from an array. What method would you use?
Answer: I’d use the filter method. It allows me to pass a condition, and it will return a new
array with only the numbers that meet that condition—like those greater than 10.
15. Q: You need to check if a variable called score holds a number. What would you do?
Answer: I’d use the typeof operator. I’d check if typeof score returns "number" to confirm that
it holds a numeric value.
16. Q: You’re storing user age in a variable, but JavaScript lets you assign a string to it later.
Why does this happen?
Answer: JavaScript is loosely typed, so a variable can hold any data type. If I store a number
first and then assign a string, it won’t throw an error unless I check types manually.
17. Q:You are developing a shopping cart feature for an e-commerce site. When a user decides
to undo their last added item, you need to both remove the last item from the cart array and
display what item was removed in a notification.
Which JavaScript array method would be most suitable for this situation, considering you need
to remove the last item and also show the user what was removed?
Answer: The pop() method. It removes the last element from the array and returns it, allowing
you to display the removed item to the user.
18. Q: What happens if you run two setTimeout functions one after another with the same
delay?
Answer: Both are asynchronous and run independently. They’ll likely execute close together,
but the order isn’t guaranteed—especially if other tasks are queued.
19. Q:Scenario: Payment Processing Question: During checkout, multiple things can go wrong -
network issues, invalid card details, server errors. How would you implement comprehensive
error handling?
Answer: I'd use try-catch blocks around all critical operations, especially API calls. Create
custom error classes for different error types like NetworkError, ValidationError, ServerError.
Use specific error messages for each scenario - "Please check your internet connection" for
network issues, "Invalid card number" for validation errors. I'd also implement retry
mechanisms for network failures and fallback options where possible.
20. Q: You have two functions with the same name in the same scope. What will happen?
Answer: The second function will overwrite the first one. In JavaScript, function declarations in
the same scope can be redefined, and only the last one is used.