JavaScript Type Conversion & Coercion Quiz

JavaScript
0 Passed
0% acceptance

Explore 50 JavaScript questions that test implicit coercion, explicit casts, string and number conversions, truthy/falsy evaluations, equality quirks, arithmetic side effects, ToPrimitive algorithms, JSON behavior, and the most common pitfalls developers hit when the engine reshapes data types.

50 Questions
~100 minutes
1

Question 1

Which built-in function converts any value to a string explicitly?

A
String(value)
B
value.toChars()
C
parseString(value)
D
JSON.stringifyWithoutQuotes(value)
2

Question 2

What does typeof return for the result of Number("42")?

A
"number"
B
"string"
C
"object"
D
"bigint"
3

Question 3

Which statement describes implicit type coercion?

A
The engine automatically converts values during operations without an explicit request.
B
Developers call a conversion function manually.
C
Values throw errors instead of converting.
D
Only strict mode files can use it.
4

Question 4

Which ECMAScript abstract operation governs how objects turn into primitives during coercion?

A
ToPrimitive
B
ToInteger
C
ToObject
D
ToJSON
5

Question 5

When does JavaScript invoke valueOf on an object?

A
During ToPrimitive when the preferred type is number
B
Only when calling JSON.stringify
C
Whenever you log the object
D
Never; valueOf is ignored
6

Question 6

What is the result of "5" - 1?

A
4
B
"51"
C
"5-1"
D
NaN
7

Question 7

How does JavaScript evaluate [] + {}?

A
"[object Object]"
B
NaN
C
0
D
Throws TypeError
8

Question 8

What does {} + [] evaluate to in most runtimes?

A
0
B
"[object Object]"
C
NaN
D
An error
9

Question 9

What is the output of the following code?

javascript
console.log(null + 5)
A
5
B
null5
C
NaN
D
Throws TypeError
10

Question 10

Which expression demonstrates implicit string coercion?

A
"Score: " + 7
B
7 - "2"
C
"5" * 2
D
Boolean(1)
11

Question 11

Which API converts a value to an integer by truncating fractional parts toward zero?

A
parseInt
B
Math.floor
C
Number.parseFloat
D
Boolean
12

Question 12

What does Number.parseFloat("12.34px") return?

A
12.34
B
"12.34px"
C
NaN
D
12
13

Question 13

How can you convert a boolean to a string without calling String()?

A
boolean + ""
B
+boolean
C
!!boolean
D
Boolean(boolean)
14

Question 14

What is the safest way to convert a string to an integer in base 10?

A
parseInt(value, 10)
B
parseInt(value)
C
Number(value, 10)
D
value | 0 without checks
15

Question 15

Which method converts numbers to strings with a specified number of decimal places?

A
num.toFixed(2)
B
String(num, 2)
C
num.toPrecision() only
D
parseFloat(num)
16

Question 16

What does String(undefined) return?

A
"undefined"
B
""
C
null
D
Throws TypeError
17

Question 17

How does template literal interpolation handle objects with custom toString?

javascript
const obj = { toString: () => 'custom' }
console.log(`Value: ${obj}`)
A
Value: custom
B
Value: [object Object]
C
Value: undefined
D
Throws TypeError
18

Question 18

Which expression coerces a number to a string by concatenation?

A
"" + 42
B
42 - ""
C
42 * "1"
D
+42
19

Question 19

Why can toString on numbers throw an error?

A
Because calling (0).toString() without parentheses may parse as a decimal literal
B
Because numbers do not implement toString
C
Because toString removes decimals
D
Because toString returns undefined
20

Question 20

What string results from ({}).toString()?

A
"[object Object]"
B
"{}"
C
"object"
D
It throws
21

Question 21

What does Number(true) evaluate to?

A
1
B
0
C
NaN
D
Throws TypeError
22

Question 22

What is the result of +"-0"?

A
-0
B
0
C
NaN
D
"-0"
23

Question 23

Which value becomes NaN when passed to Number()?

A
"foo"
B
" 42 "
C
null
D
true
24

Question 24

What does parseInt("0x10", 16) return?

A
16
B
0
C
10
D
NaN
25

Question 25

Which operator can quickly truncate a floating number to 32-bit signed integer?

A
value | 0
B
value + ""
C
!!value
D
value ** 0
26

Question 26

Which list contains only falsy values during boolean conversion?

A
false, 0, "", null, undefined, NaN
B
false, [], {}, ""
C
false, "false", 0
D
false, -1, 0
27

Question 27

What does Boolean("0") return?

A
true
B
false
C
0
D
NaN
28

Question 28

What is the boolean value of !!null?

A
false
B
true
C
null
D
undefined
29

Question 29

Which expression leverages short-circuit evaluation to assign a default only when value is falsy?

A
const result = value || "default"
B
const result = value && "default"
C
const result = value ?? "default"
D
const result = value ? value : "default"
30

Question 30

Why might you prefer ?? over || for defaults?

A
Because ?? treats 0 and "" as valid values and only falls back for null or undefined
B
Because ?? is faster
C
Because || cannot be used in strict mode
D
Because ?? converts everything to boolean first
31

Question 31

What does "5" + 3 evaluate to?

A
"53"
B
8
C
"8"
D
NaN
32

Question 32

What is the result of [] == false?

A
true
B
false
C
undefined
D
Throws TypeError
33

Question 33

Which equality operator skips type coercion?

A
===
B
==
C
=
D
=>
34

Question 34

What does "0" == false evaluate to?

A
true
B
false
C
Throws TypeError
D
undefined
35

Question 35

Which comparison causes numeric coercion on both operands?

A
"2" > 1
B
"2" + 1
C
1 + "2"
D
"2" === 2
36

Question 36

What does true * 2 evaluate to?

A
2
B
true2
C
NaN
D
Throws TypeError
37

Question 37

Which expression results in NaN due to coercion rules?

A
"foo" * 3
B
"3" * 2
C
null * 5
D
true * 4
38

Question 38

How does JavaScript evaluate "5" > "12"?

A
true
B
false
C
NaN
D
Throws TypeError
39

Question 39

What does [] == ![] evaluate to?

A
true
B
false
C
undefined
D
Throws TypeError
40

Question 40

Which comparison returns false?

A
null == undefined
B
null == 0
C
0 == false
D
"\n" == 0
41

Question 41

What does JSON.stringify({ valueOf() { return 2 } }) return?

A
"{}"
B
"2"
C
undefined
D
Throws TypeError
42

Question 42

How can you customize ToPrimitive for an object?

A
Define Symbol.toPrimitive on the object or its prototype
B
Override JSON.stringify
C
Use Object.seal
D
Set __proto__ to null
43

Question 43

What does the following code log?

javascript
const obj = {
  valueOf() {
    return 5
  },
  toString() {
    return 'seven'
  }
}
console.log(`${obj}`)
A
seven
B
5
C
[object Object]
D
Throws TypeError
44

Question 44

What is the result of +new Date("2020-01-01")?

A
The Unix timestamp for that date
B
NaN
C
A Date object
D
A string representation
45

Question 45

Which method is used by JSON.stringify when an object defines toJSON?

A
It calls toJSON if present and stringifies the returned value
B
It ignores toJSON entirely
C
It calls valueOf only
D
It requires Symbol.toPrimitive
46

Question 46

Why can using +obj where obj is an object be risky?

A
Because ToPrimitive may not return a numeric string, resulting in NaN
B
Because it throws a syntax error
C
Because objects are immutable
D
Because unary plus converts to boolean
47

Question 47

What is a common bug when using parseInt on array.map results?

A
parseInt receives the array index as the radix argument
B
parseInt cannot parse negative numbers
C
parseInt mutates the array
D
parseInt always returns NaN inside map
48

Question 48

Why should equality comparisons avoid mixing == with objects and primitives?

A
Because objects may implement custom coercion that produces surprising primitives
B
Because == is slower
C
Because objects cannot be compared at all
D
Because === throws on objects
49

Question 49

What happens when adding a number to undefined?

A
NaN
B
undefined
C
The number itself
D
Throws TypeError
50

Question 50

What is a safer strategy when dealing with user input that could be numeric or string data?

A
Validate and explicitly convert with Number(), parseInt, or parseFloat before using the value
B
Rely on implicit coercion wherever possible
C
Use == so the engine does the work
D
Store everything as JSON and parse repeatedly

QUIZZES IN JavaScript