DiscoveryVIP / Interactive learningJAVASCRIPT DISCOVERY ACADEMY

FROM YOUR FIRST INSTRUCTION TO A WORKING APPLICATION

Creating nested objects and arrays of objects

Learn JavaScript from scratch through small working examples, visual experiments and complete projects. No account or AI key required.

Predict → Experiment → Build → Test → Explain

Educator & demo scripts

Ready. Start with one instruction.

Loading the learning workspace…

Printable lesson notes · Creating nested objects and arrays of objects

Optional chaining short-circuits when the receiver is null or undefined. It prevents a missing intermediate object from causing a property-access error. It does not validate the type or quality of an existing value. Nested data requires checking each optional level. A missing inner object is different from a present field with an empty string.

Objectives

Worked example 1 · See the idea

function solve(input) {
  return input.user?.address?.city ?? "Unknown";
}
console.log(solve({"user":{"address":{"city":"Ottawa"}}}));

Expected result

Ottawa

The returned value follows the operation in the helper. Compare each element or field, including the boundary case.

Line-by-line explanation

Line 1
function solve(input) {

Define a reusable function. Names inside its parentheses are parameters; actual arguments arrive when it is called. Defining the function does not run it.

Line 2
return input.user?.address?.city ?? "Unknown";

Evaluate the expression after return, end this function call, and give that value to the caller. The result is not automatically displayed.

Line 3
}

Close the current instruction block.

Line 4
console.log(solve({"user":{"address":{"city":"Ottawa"}}}));

Evaluate the argument and display its value at this point. Quoted text is literal; an unquoted identifier reads its current binding.

Worked example 2 · Change the value

function solve(input) {
  return input.user?.address?.city ?? "Unknown";
}
console.log(solve({}));

Expected result

Unknown

The returned value follows the operation in the helper. Compare each element or field, including the boundary case.

Line-by-line explanation

Line 1
function solve(input) {

Define a reusable function. Names inside its parentheses are parameters; actual arguments arrive when it is called. Defining the function does not run it.

Line 2
return input.user?.address?.city ?? "Unknown";

Evaluate the expression after return, end this function call, and give that value to the caller. The result is not automatically displayed.

Line 3
}

Close the current instruction block.

Line 4
console.log(solve({}));

Evaluate the argument and display its value at this point. Quoted text is literal; an unquoted identifier reads its current binding.

Worked example 3 · Explore a boundary

function solve(input) {
  return input.user?.address?.city ?? "Unknown";
}
console.log(solve({"user":{"address":{"city":""}}}));

Expected result

No console output.

Optional chaining short-circuits when the receiver is null or undefined. It prevents a missing intermediate object from causing a property-access error. It does not validate the type or quality of an existing value. Nested data requires checking each optional level. A missing inner object is different from a present field with an empty string. This boundary uses the explicitly shown argument; compare the exact result rather than assuming it matches the earlier empty-input case.

Line-by-line explanation

Line 1
function solve(input) {

Define a reusable function. Names inside its parentheses are parameters; actual arguments arrive when it is called. Defining the function does not run it.

Line 2
return input.user?.address?.city ?? "Unknown";

Evaluate the expression after return, end this function call, and give that value to the caller. The result is not automatically displayed.

Line 3
}

Close the current instruction block.

Line 4
console.log(solve({"user":{"address":{"city":""}}}));

Evaluate the argument and display its value at this point. Quoted text is literal; an unquoted identifier reads its current binding.

Independent exercise

Repair the intentionally changed operation so the example produces the exact expected result. Then try the extension without copying the answer.

function solve(input) {
  return undefined; // input.user?.address?.city ?? "Unknown";
}
console.log(solve({"user":{"address":{"city":"Ottawa"}}}));
Reference solution
function solve(input) {
  return input.user?.address?.city ?? "Unknown";
}
console.log(solve({"user":{"address":{"city":"Ottawa"}}}));

Hints

  1. Compare the observed result with the expected result. Read the relevant source line aloud.
  2. The operation to inspect is: return
  3. Use return in place of return undefined; // . Rerun and explain why the change works.

Professional tip and common mistake

Return Unknown when the city path is missing.

Reflect and extend

Which operation changed the result, why did it work, and what would you test next?

Use a different value that could expose a boundary or mistake. Predict the result first, run it, and explain how the concept would help in a small real application.

Related: Previous lesson Next lesson

Course syllabus and printable lesson notes

100 lessons across 20 modules, covering variables, operators, strings, conditions, loops, functions, scope, arrays, objects, DOM, events, state, testing, asynchronous JavaScript and application development.

1. Your First Steps into Programming
  1. What JavaScript is and what you can build with it
  2. Understanding instructions, code, and programs
  3. Running JavaScript and using the console
  4. Understanding syntax, comments, and basic code structure
  5. Building your first interactive JavaScript experiment
2. VARIABLES AND INFORMATION
  1. Understanding variables as containers for information
  2. Declaring variables with let
  3. Using const and understanding reassignment
  4. Naming variables and writing readable code
  5. Changing values and tracking program state
3. JAVASCRIPT DATA TYPES
  1. Understanding strings
  2. Understanding numbers
  3. Understanding Boolean values
  4. Understanding null and undefined
  5. Identifying and converting data types
4. OPERATORS AND CALCULATIONS
  1. Basic arithmetic operations
  2. Operator precedence and parentheses
  3. Assignment and compound assignment
  4. Increment, decrement, remainder, and practical calculations
  5. Building useful mathematical expressions
5. WORKING WITH TEXT
  1. Creating and combining strings
  2. Understanding template literals
  3. Measuring and accessing string characters
  4. Searching, slicing, replacing, and transforming text
  5. Building practical text-processing applications
6. MAKING DECISIONS
  1. Comparison operators
  2. Understanding equality and strict equality
  3. Writing if and else statements
  4. Working with else if and multiple conditions
  5. Logical operators and the ternary operator
7. REPEATING INSTRUCTIONS
  1. Understanding repetition and loops
  2. Writing for loops
  3. Writing while and do...while loops
  4. Using break and continue
  5. Nested loops and repeated patterns
8. FUNCTIONS AND REUSABLE CODE
  1. Understanding what functions do
  2. Defining and calling functions
  3. Using parameters and arguments
  4. Returning values
  5. Function expressions and arrow functions
9. SCOPE AND EXECUTION
  1. Understanding global and local scope
  2. Block scope with let and const
  3. Understanding the call stack
  4. Nested functions and introductory closures
  5. Organizing programs into smaller responsibilities
10. ARRAYS AND COLLECTIONS
  1. Creating and understanding arrays
  2. Accessing elements using indexes
  3. Adding, removing, and updating elements
  4. Iterating over arrays
  5. Searching, sorting, and organizing arrays
11. MODERN ARRAY METHODS
  1. Transforming data with map()
  2. Selecting data with filter()
  3. Finding elements with find() and related methods
  4. Accumulating values with reduce()
  5. Combining array methods into transformation pipelines
12. OBJECTS AND STRUCTURED DATA
  1. Understanding JavaScript objects
  2. Accessing and changing object properties
  3. Creating nested objects and arrays of objects
  4. Understanding references and mutation
  5. Using destructuring, spread syntax, and object methods
13. JAVASCRIPT AND WEB PAGES
  1. Understanding HTML, CSS, JavaScript, and the browser
  2. Understanding the Document Object Model
  3. Selecting elements with JavaScript
  4. Updating content, attributes, and CSS classes
  5. Creating, inserting, and removing elements
14. EVENTS AND USER INTERACTION
  1. Understanding browser events
  2. Working with click and input events
  3. Handling forms and user submissions
  4. Understanding event objects and event propagation
  5. Building accessible interactive interfaces
15. PRACTICAL WEB APPLICATIONS
  1. Understanding application state
  2. Rendering data into the interface
  3. Building reusable interface components
  4. Saving information with localStorage
  5. Combining state, events, and persistence
16. ERRORS, DEBUGGING, AND TESTING
  1. Understanding syntax, runtime, and logic errors
  2. Reading error messages and console output
  3. Debugging with breakpoints and state inspection
  4. Handling exceptions with try and catch
  5. Writing basic tests and checking edge cases
17. ASYNCHRONOUS JAVASCRIPT
  1. Understanding synchronous and asynchronous code
  2. Timers and delayed execution
  3. Understanding promises
  4. Using async and await
  5. Understanding the event loop, tasks, and microtasks
18. APIS AND EXTERNAL DATA
  1. Understanding APIs and HTTP
  2. Working with JSON
  3. Retrieving information using fetch()
  4. Handling loading states, errors, and unsuccessful requests
  5. Building an interactive application using API data
19. MODERN JAVASCRIPT DEVELOPMENT
  1. Organizing code into modules
  2. Understanding classes and object-oriented programming
  3. Writing maintainable and reusable JavaScript
  4. Understanding browser performance and efficient updates
  5. Building responsibly with AI-assisted coding
20. BECOMING AN INDEPENDENT DEVELOPER
  1. Turning an application idea into requirements
  2. Planning features, data, and interface behavior
  3. Building and testing an initial implementation
  4. Debugging, improving, and extending an application
  5. Completing and publishing your JavaScript capstone project