Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    JSJavaScript

    Getting started

    • What is JavaScript?
    • Variables: let, const and var
    • Data types
    • Type conversion

    Operators and strings

    • Arithmetic and assignment
    • Comparison and logic
    • Strings and template literals
    • String methods
    • Numbers and Math

    Control flow

    • Conditions: if and else
    • switch and the ternary operator
    • Loops: for and while
    • Challenge: FizzBuzz

    Functions

    • Functions
    • Arrow functions
    • Parameters: default, rest and spread
    • Scope and closures
    • Callbacks and higher-order functions

    Arrays

    • Array basics
    • map, filter and find
    • reduce and sorting
    • Destructuring

    Objects

    • Object basics
    • Copies and references
    • Methods and this
    • JSON and optional chaining

    Classes and OOP

    • Classes
    • Inheritance

    JavaScript in the page

    • Controlling the page (DOM)
    • Creating elements
    • Events
    • Forms and validation
    • Saving in the browser (localStorage)

    Errors and async

    • Handling errors: try and catch
    • Timers and the event loop
    • Promises
    • async and await
    • Fetching data from an API

    Practical tools and techniques

    • Searching with regular expressions
    • Cancellation and stale results
    • Testing and debugging JavaScript

    Modern JavaScript and a project

    • Map and Set
    • Dates and localised formatting
    • Modules: import and export
    • Project: a complete to-do app
    JavaScript

    Objects · Lesson 26 of 45

    JSON and optional chaining

    How data travels as text, and reading data that might be missing.

    8 minutes3 games

    JSON is a way to write data as text, and the language websites and servers speak. It looks like an object except: keys must be in "...", and there are no functions or undefined.

    • JSON.stringify(obj) turns an object into text.
    • JSON.parse(text) turns the text back into an object.

    a JavaScript object

    JSON.stringify → text

    sent to a server or saved

    JSON.parse → object

    JavaScript
    const order = { id: 7, items: ["pen", "mug"], paid: true }
    const text = JSON.stringify(order)
    console.log(text)
    console.log(typeof text)
    
    const back = JSON.parse(text)
    console.log(back.items[1])
    
    console.log(JSON.stringify(order, null, 2))

    Optional chaining ?.

    When data might be missing (say from an API), user.address.city throws if address doesn't exist. ?. stops and returns undefined instead of an error.

    JavaScript
    const user = { name: "Ali" }
    
    console.log(user.address?.city)               // undefined, no error
    console.log(user.address?.city ?? "Unknown")  // Unknown
    console.log(user.greet?.())                   // call only if it exists
    console.log(user.tags?.[0])

    Important

    JSON.parse throws on invalid JSON, so wrap it in try/catch when the data comes from outside. Date values become strings in stringify and don't turn back into dates by themselves.

    Interview question

    What gets lost when you JSON.stringify an object?

    What's the output?

    const data = JSON.parse('{"n": 5}')
    console.log(data.n + 1)

    What will this code print?

    Spot the bug

    One line throws if there's no profile. Which?

    Exercise

    Parse the text and print the city (Luxor).

    Expected output: Luxor

    JavaScript
    const text = '{"name":"Adam","address":{"city":"Luxor"}}'
    

    Learn more from

    • javascript.info: JSON methodsjavascript.info
    • javascript.info: Optional chainingjavascript.info
    • MDN: Working with JSONdeveloper.mozilla.org

    Played 0 of 3 games

    Sign in to save your progress and earn XP.

    PreviousMethods and thisNextClasses
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.