Open code, practice, and notes →
Download source with English comments · for reading
JavaScript · 03 Equality Comparisons and Defaults
Series contents · Technology map
Stage: Values and calculations · Editable example · Storage ID: javascript-step-03
Goals and learning outcomes
- Record 8 default-value outputs.
- Explain the comparison results for the same reference and different references.
Prerequisites
The work and experiment records from Unit 2, “String input and number conversion”
Key concepts and explanations
Equality Comparisons
=== compares without type coercion. Objects with the same contents are different if their references differ.
Nullish Defaults
?? applies a default only to null and undefined. Its results differ from logical OR when you want to preserve 0 and an empty string.
Practice setup
First check the execution scope guidance on the example screen. The following environment is for extending the assignment in a separate project. Implement it yourself in a separate local project and check it with browser developer tools. In a separate folder, connect your practice JS from index.html using script type="module" and open it through a local HTTP server. Use the Console, Network, and Elements panels.
The linked example is a starting point. It does not implement every task below. Complete work beyond its supported scope in a separate project.
Practice tasks
- In defaults.js, prepare 0, an empty string, null, undefined, and 2 objects.
- Apply the default value 10 using logical OR and ??, and also output the === comparison of objects with identical contents.
- Run with 0, an empty string, null, undefined, and 2 objects. Results to check: Record 8 default-value outputs. Explain the comparison results for the same reference and different references. Record expected and actual results separately.
Variables to observe
| Variable | Baseline | Change | Expected change / hypothesis |
|---|---|---|---|
| Operator | Logical OR | ?? | Hypothesis before running: The setting of 0 will be preserved. Keep the other conditions fixed and record the actual result separately after running. |
Predictions in this table are not measured results. Change one condition at a time and record why the actual result differs.
Completion criteria
- Record 8 default-value outputs.
- Explain the comparison results for the same reference and different references.
Self-check
- What settings can validly be 0?
- Why does checking content equality with === fail?
Official references
Linked example and execution scope
Example source · Runtime environments and limits
Compare the editable inputs with actual browser execution results.
Source code and records are not overwritten automatically. Resetting an existing example also preserves your notes.
Next learning
Previous: String input and number conversion · Next: Branches and Early Returns