JavaScript JavaScript 5 — Questions and Answers
Question 1: What is the output of `console.log(typeof function(){})` in JavaScript?
- 'object'
- 'function' (Correct answer)
- 'undefined'
- 'callable'
Correct answer: 'function'
`typeof` returns `'function'` for function objects, making them distinguishable from plain objects.
Question 2: What does the `this` keyword refer to inside an arrow function?
- The arrow function itself
- The global object (window or global)
- The `this` value of the enclosing lexical scope (Correct answer)
- It is always undefined in arrow functions
Correct answer: The `this` value of the enclosing lexical scope
Arrow functions do not have their own `this` binding — they inherit `this` from the surrounding lexical context where they are defined.
Question 3: Which of the following correctly creates a new Promise that resolves after 1 second?
- new Promise(resolve => setTimeout(resolve, 1000)) (Correct answer)
- Promise.resolve(setTimeout(1000))
- new Promise(() => wait(1000))
- Promise.delay(1000)
Correct answer: new Promise(resolve => setTimeout(resolve, 1000))
You wrap `setTimeout` inside a `new Promise` constructor and call `resolve` inside the timeout callback.
Question 4: What is prototype-based inheritance in JavaScript?
- A system where objects can only inherit from classes defined with the `class` keyword
- A system where objects inherit directly from other objects via a prototype chain (Correct answer)
- A mechanism to copy all properties from one object to another
- A way to restrict property access using private fields
Correct answer: A system where objects inherit directly from other objects via a prototype chain
JavaScript uses prototype-based inheritance where each object has an internal `[[Prototype]]` link to another object from which it inherits properties.
Question 5: What does `localStorage.setItem('key', value)` require about the value?
- It must be a number
- It must be a string — non-string values are automatically converted (Correct answer)
- It must be a plain object
- It must be serialized with JSON.stringify before storage
Correct answer: It must be a string — non-string values are automatically converted
`localStorage` can only store strings; passing a non-string value causes it to be coerced via `.toString()`, which is why objects should be stringified manually.
Question 6: What is the purpose of the `try...catch...finally` block in JavaScript?
- To define multiple fallback functions for asynchronous code
- To handle runtime errors — `catch` runs on error, `finally` always runs (Correct answer)
- To retry failed operations automatically a set number of times
- To declare variables that are only accessible within error handlers
Correct answer: To handle runtime errors — `catch` runs on error, `finally` always runs
`try` wraps code that may throw, `catch` handles thrown errors, and `finally` executes regardless of whether an error occurred.
Question 7: Which method would you use to check whether a string contains a specific substring in modern JavaScript?
- String.prototype.search()
- String.prototype.indexOf()
- String.prototype.includes() (Correct answer)
- String.prototype.match()
Correct answer: String.prototype.includes()
`String.prototype.includes()` returns a boolean indicating whether the given substring is found within the string, providing a cleaner API than `indexOf() !== -1`.
What is the output of `console.log(typeof function(){})` in JavaScript?