JavaScript Core Syntax 2 — Questions and Answers
Question 1: What is the difference between `==` and `===` in JavaScript?
- `==` checks value only; `===` checks value and type (Correct answer)
- `==` checks type only; `===` checks value and type
- There is no difference
- `===` is faster but less accurate
Correct answer: `==` checks value only; `===` checks value and type
`==` performs type coercion before comparison (loose equality), while `===` checks both value and type without coercion (strict equality). For example, `'5' == 5` is `true` but `'5' === 5` is `false`.
With `==`, JavaScript tries to convert operands to the same type before comparing. This can produce counterintuitive results: `null == undefined` is `true`, `0 == false` is `true`, `'' == false` is `true`. The `===` operator never performs type coercion — if the types differ, it immediately returns `false`. Best practice is to use `===` by default.
Question 2: What does the `typeof` operator return for `null`?
- 'null'
- 'undefined'
- 'object' (Correct answer)
- 'empty'
Correct answer: 'object'
`typeof null` returns `'object'`. This is a well-known quirk in JavaScript originating from its original implementation. `null` is a primitive value, but `typeof null === 'object'` is a long-standing bug in the language.
In the first version of JavaScript, values were represented as a type tag and a value. The type tag for objects was `0`, and `null` was represented as the null pointer (`0x00`), which also had a type tag of `0`. As a result, `typeof null` incorrectly returns `'object'`. This bug has never been fixed to avoid breaking existing code.
Question 3: What is the output of `typeof undefined`?
- 'null'
- 'undefined' (Correct answer)
- 'void'
- 'nothing'
Correct answer: 'undefined'
`typeof undefined` returns the string `'undefined'`. This is consistent and expected — `undefined` is the type of variables that have been declared but not assigned.
`undefined` is a primitive value that is automatically assigned to variables that have been declared but not initialized, to missing function arguments, and to object properties that don't exist. `typeof undefined === 'undefined'` is `true` and is one of the safe ways to check for undefined (safe even for undeclared variables).
Question 4: What is the result of `0.1 + 0.2 === 0.3` in JavaScript?
- true
- false (Correct answer)
- TypeError
- undefined
Correct answer: false
Due to floating-point precision issues in IEEE 754 binary representation, `0.1 + 0.2` evaluates to `0.30000000000000004`, which is not strictly equal to `0.3`. So the expression is `false`.
JavaScript uses 64-bit IEEE 754 double-precision floating-point numbers. Neither `0.1` nor `0.2` can be represented exactly in binary floating point, so their sum has a rounding error. The correct way to compare floating-point numbers is to check if the absolute difference is smaller than a tolerance: `Math.abs(0.1 + 0.2 - 0.3) < Number.EPSILON`.
Question 5: What does the `in` operator do in JavaScript?
- Checks if a value exists in an array
- Checks if a property exists in an object or its prototype chain (Correct answer)
- Iterates over object properties
- Imports a module
Correct answer: Checks if a property exists in an object or its prototype chain
The `in` operator returns `true` if the specified property exists in the given object or anywhere in its prototype chain.
`'prop' in obj` returns `true` if `obj` has a property named `'prop'`, either as its own property or inherited via prototype chain. To check only own properties, use `Object.prototype.hasOwnProperty()` or `Object.hasOwn()`. The `in` operator is commonly used in `for...in` loops to iterate enumerable properties.
Question 6: What is the result of `!!'hello'`?
- false
- true (Correct answer)
- 'hello'
- undefined
Correct answer: true
The double negation `!!` converts a value to its boolean equivalent. Non-empty strings are truthy in JavaScript, so `!!'hello'` evaluates to `true`.
The first `!` converts `'hello'` to `false` (negation of a truthy value). The second `!` converts `false` back to `true`. So `!!value` effectively casts any value to a boolean. Truthy values include non-empty strings, non-zero numbers, objects, and arrays. Falsy values are `false`, `0`, `''`, `null`, `undefined`, and `NaN`.
What is the difference between `==` and `===` in JavaScript?