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

    JavaScript in the page · Lesson 33 of 45

    Saving in the browser (localStorage)

    Keep data after the page is closed.

    7 minutes2 games

    localStorage saves text in the browser that survives closing the page and the browser. Its tools: setItem(key, value), getItem(key), removeItem(key). It stores only strings, so objects go through JSON.

    HTML
    <p>Visits on this device: <strong id="visits"></strong></p>
    <button id="reset">Reset</button>
    
    <script>
      let visits = 0
      try {
        visits = Number(localStorage.getItem("visits") ?? 0) + 1
        localStorage.setItem("visits", String(visits))
      } catch {
        console.log("Storage is blocked in this preview")
      }
      document.querySelector("#visits").textContent = visits
      document.querySelector("#reset").onclick = () => {
        try { localStorage.removeItem("visits") } catch {}
        document.querySelector("#visits").textContent = 0
      }
    
      const settings = { theme: "dark", fontSize: 16 }
      const saved = JSON.stringify(settings)
      console.log("saved as text:", saved)
      console.log("read back:", JSON.parse(saved).theme)
    </script>

    Storage may be blocked in this preview for security, so the code handles it with try/catch.

    localStoragesessionStorageCookies

    Lasts until

    you clear it

    the tab closes

    an expiry date

    Sent to server?

    no

    no

    with every request

    Size

    ~5MB

    ~5MB

    ~4KB

    Important

    Never store secrets (passwords, important tokens) in localStorage: any JavaScript running on the page can read it.

    Interview question

    What's the difference between localStorage, sessionStorage and cookies?

    Complete the code

    Save the object to localStorage.

    const cart = { items: 3 }
    localStorage.setItem("cart", JSON.(cart))

    Quick question

    localStorage.setItem("n", 5) then localStorage.getItem("n") returns…?

    Learn more from

    • javascript.info: LocalStorage, sessionStoragejavascript.info
    • MDN: Window.localStoragedeveloper.mozilla.org

    Played 0 of 2 games

    Sign in to save your progress and earn XP.

    PreviousForms and validationNextHandling errors: try and catch
    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.