Sparround

Types, variables and comparison

In JavaScript, variables are declared with let (mutable) and const (non-reassignable). var is legacy — don't use it.

Core primitive types: string, number, boolean, null, undefined. Knowing the difference matters:

  • undefined — a value was never assigned (e.g. a missing object field)
  • null — intentional absence of a value

The typeof operator checks types: typeof 42"number". Famous quirk: typeof null"object" (a historical bug).

Comparison: always use === (strict equality). == coerces types and gives surprises: 0 == ''true, but 0 === ''false.

Truthy/falsy: in a condition, every value converts to boolean. There are exactly 6 falsy values: false, 0, '', null, undefined, NaN. Everything else is truthy — including an empty array [] and empty object {}!

In test code this is a classic source of assertion mistakes: if (results) is true even for an empty array — you need if (results.length > 0).

ExpressionResultWhy
0 == ''true== coerces both to number
0 === ''falsetypes differ, no coercion
Boolean([])truean empty array is truthy
null == undefinedtruespecial rule for ==; false with ===
NaN === NaNfalseNaN equals nothing — use Number.isNaN()

Template literals are daily bread in test code: ` Hello, ${userName}! — the main tool for dynamic locators and test names: page.locator([data-id="${productId}"]) `.

📚 Sources and documentation