JavaScript JavaScript 4 — Questions and Answers
Question 1: What is the difference between `==` and `===` in JavaScript?
- `==` checks value only; `===` checks value AND type (Correct answer)
- `==` is faster; `===` is slower but more accurate
- `==` works only on primitives; `===` works on all types
- There is no difference — they produce identical results
Correct answer: `==` checks value only; `===` checks value AND type
`==` performs type coercion before comparing, while `===` (strict equality) requires both value and type to match.
Question 2: What will `console.log([] + [])` output in JavaScript?
- []
- 0
- '' (Correct answer)
- null
Correct answer: ''
Both arrays are coerced to empty strings, and concatenating two empty strings yields an empty string `''`.
Question 3: Which built-in method removes and returns the LAST element of an array?
- shift()
- pop() (Correct answer)
- splice()
- slice()
Correct answer: pop()
`Array.prototype.pop()` removes the last element from an array and returns that element, mutating the original array.
Question 4: What is event delegation in JavaScript?
- Passing an event object from one function to another
- Attaching a single event listener to a parent element to handle events from its children (Correct answer)
- Preventing an event from bubbling up the DOM tree
- Scheduling events to fire after a delay
Correct answer: Attaching a single event listener to a parent element to handle events from its children
Event delegation attaches one listener to a parent element and uses the event's `target` property to identify which child triggered it, improving performance.
Question 5: What does `Array.isArray([])` return?
- false
- true (Correct answer)
- 'array'
- undefined
Correct answer: true
`Array.isArray()` reliably returns `true` for arrays, unlike `typeof` which returns `'object'` for arrays.
Question 6: What is the purpose of `async/await` in JavaScript?
- To run code in parallel threads
- To write asynchronous Promise-based code in a synchronous-looking style (Correct answer)
- To block the main thread until a task completes
- To replace callbacks and promises entirely with a new async model
Correct answer: To write asynchronous Promise-based code in a synchronous-looking style
`async/await` is syntactic sugar over Promises that lets you write asynchronous code that reads like synchronous code without blocking.
Question 7: Which method converts a JavaScript object into a JSON string?
- JSON.parse()
- JSON.stringify() (Correct answer)
- Object.toString()
- JSON.encode()
Correct answer: JSON.stringify()
`JSON.stringify()` serializes a JavaScript value into a JSON string, with optional parameters for filtering and formatting.
What is the difference between `==` and `===` in JavaScript?