AI InterviewSearch…Search problems, topics, or pages…Ctrl KGet started
Skip to content

02 Values, types and coercion / Checkpoint

Checkpoint — Values, types and coercion

24 min · +25 XP · 3 exercises · 12 questions

Exercises0/3
Quiz · 70% to clear12 q

This checkpoint covers the seven primitives and typeof, numbers as doubles with NaN, safe integers and BigInt, strings and their toolkit, truthiness, the coercion rules behind ==, + and the comparison operators, the nullish operators, and let/const/var with hoisting and the temporal dead zone.

How it works. Twelve questions and three programs; 70% on the questions and every program accepted clears the module.

Before you start, make sure you can answer:

  • The two typeof traps and how to test for null and for arrays.
  • Why 0.1 + 0.2 !== 0.3, how to test for NaN, and where exact integers end.
  • Number("42px") versus parseInt("42px", 10); slice versus substring; why an emoji has length 2.
  • The eight falsy values; || versus ??; the rules == applies in order.
  • What + does when one side is a string, and how < compares two strings.
  • var versus let in a loop with callbacks, and what the temporal dead zone is.

The programs are a type-inspector that classifies input tokens the way the language would, a safe-arithmetic calculator that switches to BigInt when a result would lose precision, and a coercion table generator that prints what ==, === and + produce for pairs of values.

Common questions

In what order does == convert its operands in JavaScript?

If the types match, == behaves like ===. Otherwise null and undefined equal each other and nothing else; a string compared with a number becomes a number; a boolean becomes a number; and an object compared with a primitive is converted with valueOf or toString.

How does the less-than operator compare two strings in JavaScript?

When both sides are strings, < compares them by UTF-16 code units rather than numerically, so "10" < "9" and "Z" < "a" are both true. If either side is not a string, both are converted to numbers first.

Where do exact integers end in JavaScript?

At Number.MAX_SAFE_INTEGER, which is 2⁵³ − 1. Past it, doubles start skipping integers — 2 ** 53 + 1 evaluates to 2 ** 53 — so arithmetic silently rounds; a program that must stay exact should switch to BigInt.

JavaScript Types Quiz: Coercion, Equality and NaN Practice

Test your grasp of JavaScript values with 12 questions and three programs on typeof, NaN, BigInt, string methods, truthiness, == vs === and let vs var.

  • Course: JavaScript study plan
  • Module: Values, types and coercion
  • Kind: Checkpoint — cleared at 70%
  • Reading time: 24 min
  • Runtime: Node 16

Checkpoint — Values, types and coercion is the checkpoint that closes the Values, types and coercion module: a graded quiz and whole-program exercises, passed at 70%.

Instructions

This checkpoint covers the seven primitives and typeof, numbers as doubles with NaN, safe integers and BigInt, strings and their toolkit, truthiness, the coercion rules behind ==, + and the comparison operators, the nullish operators, and let/const/var with hoisting and the temporal dead zone.

How it works. Twelve questions and three programs; 70% on the questions and every program accepted clears the module.

Before you start, make sure you can answer:

  • The two typeof traps and how to test for null and for arrays.
  • Why 0.1 + 0.2 !== 0.3, how to test for NaN, and where exact integers end.
  • Number("42px") versus parseInt("42px", 10); slice versus substring; why an emoji has length 2.
  • The eight falsy values; || versus ??; the rules == applies in order.
  • What + does when one side is a string, and how < compares two strings.
  • var versus let in a loop with callbacks, and what the temporal dead zone is.

The programs are a type-inspector that classifies input tokens the way the language would, a safe-arithmetic calculator that switches to BigInt when a result would lose precision, and a coercion table generator that prints what ==, === and + produce for pairs of values.

Common questions

In what order does == convert its operands in JavaScript?

If the types match, == behaves like ===. Otherwise null and undefined equal each other and nothing else; a string compared with a number becomes a number; a boolean becomes a number; and an object compared with a primitive is converted with valueOf or toString.

How does the less-than operator compare two strings in JavaScript?

When both sides are strings, < compares them by UTF-16 code units rather than numerically, so "10" < "9" and "Z" < "a" are both true. If either side is not a string, both are converted to numbers first.

Where do exact integers end in JavaScript?

At Number.MAX_SAFE_INTEGER, which is 2⁵³ − 1. Past it, doubles start skipping integers — 2 ** 53 + 1 evaluates to 2 ** 53 — so arithmetic silently rounds; a program that must stay exact should switch to BigInt.

Exercises

Type inspector

Read n tokens and classify each with the parse helper: print <token>: <kind> where the kind is null for null, array for arrays, and otherwise typeof; for numbers append nan=<Number.isNaN> safe=<Number.isSafeInteger>.

Example: 4 then null [1] 3.5 NaN →

null: null
[1]: array
3.5: number nan=false safe=false
NaN: number nan=true safe=false

A calculator that never loses digits

Read lines <a> <op> <b> (integers as text, op one of + - *) until end of input. Compute with Number when both operands and the result are safe integers; otherwise recompute with BigInt. Print <a> <op> <b> = <result> mode=<number|bigint>.

Example input

2 + 3
9007199254740991 + 1

→

2 + 3 = 5 mode=number
9007199254740991 + 1 = 9007199254740992 mode=bigint

The coercion table

Read n pairs of tokens (via parse) and print, for each, <a> <b> | ==:<a == b> ===:<a === b> +:<repr of a + b> (<typeof a + b>) where the repr is JSON.stringify for strings and arrays and String otherwise. Predict every line before running.

Example: 3 then 1 one, null 0, [] 0 →

1 one | ==:false ===:false +:"1one" (string)
null 0 | ==:false ===:false +:0 (number)
[] 0 | ==:true ===:false +:"0" (string)

In this module: Values, types and coercion

← let, const and var — scope, hoisting and the temporal dead zone · Declaring functions — declarations, expressions, arrows and parameters →