JavaScript JavaScript 2 — Questions and Answers
Question 1: What does the `typeof null` expression return in JavaScript?
- 'null'
- 'undefined'
- 'object' (Correct answer)
- 'boolean'
Correct answer: 'object'
`typeof null` returns 'object' due to a long-standing bug in JavaScript's original implementation.
Question 2: Which array method creates a new array by calling a function on every element of the original array?
- forEach()
- filter()
- map() (Correct answer)
- reduce()
Correct answer: map()
`Array.prototype.map()` returns a new array with each element transformed by the provided callback.
Question 3: What is the output of `console.log(0.1 + 0.2 === 0.3)` in JavaScript?
- true
- false (Correct answer)
- NaN
- undefined
Correct answer: false
Due to floating-point precision limitations, `0.1 + 0.2` equals `0.30000000000000004`, not exactly `0.3`.
Question 4: What does the `Array.prototype.reduce()` method do?
- Removes duplicate elements from an array
- Filters elements based on a condition
- Executes a reducer function on each element, accumulating a single result (Correct answer)
- Sorts array elements in ascending order
Correct answer: Executes a reducer function on each element, accumulating a single result
`reduce()` applies a callback with an accumulator to each element, ultimately returning a single accumulated value.
Question 5: Which keyword is used to define a named function expression in JavaScript?
- def
- func
- function (Correct answer)
- lambda
Correct answer: function
JavaScript uses the `function` keyword to define both function declarations and function expressions.
Question 6: What does `JSON.stringify()` do to `undefined` values inside an object?
- Converts them to null
- Throws a TypeError
- Omits the key-value pair entirely (Correct answer)
- Converts them to the string 'undefined'
Correct answer: Omits the key-value pair entirely
`JSON.stringify()` omits object properties with `undefined` values because undefined is not a valid JSON value.
Question 7: What is the purpose of the `Symbol` type introduced in ES6?
- To represent mathematical symbols like π
- To create unique and immutable identifiers (Correct answer)
- To define private class fields
- To replace string-based object keys
Correct answer: To create unique and immutable identifiers
`Symbol()` creates a guaranteed unique, immutable primitive value often used as unique object property keys.
What does the `typeof null` expression return in JavaScript?