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

    Classes and OOP · Lesson 27 of 45

    Classes

    A template for making many objects of the same shape.

    9 minutes3 games

    A class is a blueprint. Instead of hand-writing an object for every student, you define the shape once and create copies (instances) with new. The constructor runs when an instance is created and sets up its data.

    JavaScript
    class Student {
      constructor(name, level = 1) {
        this.name = name
        this.level = level
        this.xp = 0
      }
    
      earn(points) {
        this.xp += points
        if (this.xp >= 100) {
          this.level++
          this.xp -= 100
        }
      }
    
      get title() {
        return `${this.name} (level ${this.level})`
      }
    }
    
    const a = new Student("Laila")
    const b = new Student("Karim", 3)
    a.earn(130)
    console.log(a.title, a.xp)
    console.log(b.title)
    • class Student
      • a = new Student("Laila")
        • name: "Laila"
        • level: 2
      • b = new Student("Karim", 3)
        • name: "Karim"
        • level: 3

    One blueprint, many instances, each with its own data.

    get title() is a getter: you use it like a property, a.title, with no brackets, but it's computed each time.

    Private fields #

    JavaScript
    class Wallet {
      #balance = 0          // private: not reachable from outside
    
      deposit(amount) {
        if (amount <= 0) throw new Error("Amount must be positive")
        this.#balance += amount
      }
    
      get balance() {
        return this.#balance
      }
    }
    
    const w = new Wallet()
    w.deposit(200)
    console.log(w.balance)
    console.log(w["#balance"])  // undefined

    Important

    JavaScript classes are syntactic sugar over prototypes: all instances share the same methods through the prototype instead of each getting its own copy.

    Interview question

    What are the 4 core principles of OOP?

    What's the output?

    class Counter {
      constructor() {
        this.n = 0
      }
      inc() {
        this.n++
        return this
      }
    }
    console.log(new Counter().inc().inc().inc().n)

    What will this code print?

    Complete the code

    Complete the class and create an instance.

    class Book {
      (title) {
        this.title = title
      }
    }
    const b =  Book("JS 101")

    Exercise

    Make a Rectangle class with width, height and an area() method. Print new Rectangle(5, 4).area() (20).

    Expected output: 20

    JavaScript

    Learn more from

    • javascript.info: Class basic syntaxjavascript.info
    • MDN: Classesdeveloper.mozilla.org
    • MDN: Private propertiesdeveloper.mozilla.org

    Played 0 of 3 games

    Sign in to save your progress and earn XP.

    PreviousJSON and optional chainingNextInheritance
    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.