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).
| Expression | Result | Why |
|---|---|---|
| 0 == '' | true | == coerces both to number |
| 0 === '' | false | types differ, no coercion |
| Boolean([]) | true | an empty array is truthy |
| null == undefined | true | special rule for ==; false with === |
| NaN === NaN | false | NaN 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
- Grammar and typesofficialdeveloper.mozilla.org
- Equality comparisons and samenessofficialdeveloper.mozilla.org
The precise official account of == versus ===, with tables.