JavaScript is object-based. Its own objects, and the objects that the browser provides, let scripts model data and control the window.

JavaScript Objects

An object is a collection of properties (name-value pairs) and methods (functions): const student = { name: "Asha", marks: 88, grade() { return this.marks >= 80 ? "A" : "B"; } };. Properties are read with the dot (student.name) or brackets (student["name"]). Classes (ES6) give a template for objects, with a constructor: class Student { constructor(n) { this.name = n; } }. Destructuring extracts values: const { name, marks } = student;. The spread operator ... copies arrays and objects.

Definition: Browser Object Model. The set of objects provided by the browser, such as window, location, history, navigator and screen, through which JavaScript can control the browser window.

Built-in Objects

JavaScript provides built-in objects. Math gives constants and functions such as Math.PI and Math.random(). Date handles dates. String, Number and Boolean wrap primitive values with methods, such as Number(3.14159).toFixed(2). Array gives list methods. JSON converts objects to and from text. RegExp represents regular expressions. Promise represents a value that will be ready later, used with async and await.

Controlling the Browser (BOM)

The window object represents the browser window and is the global object: window.innerWidth and innerHeight give the size, window.open() opens a new window, and setTimeout() and setInterval() schedule code. location holds the URL: location.href gives or changes it (navigating to another page), and location.reload() reloads. history moves through visited pages: history.back() and history.forward(). navigator describes the browser, such as navigator.language and navigator.onLine. screen gives the screen size. localStorage and sessionStorage store small amounts of data in the browser.

Combining HTML, CSS and JavaScript

A complete interactive feature uses all three: HTML for the elements (such as a button and a box), CSS for their look and for states such as .hidden or .dark, and JavaScript to listen for events and switch classes or content. For example, a "Dark mode" button toggles a dark class on the body, and the CSS for .dark changes the colours.

Table 4.2: Browser objects

Object Represents Example
window Browser window window.innerWidth
location Current URL location.href = "home.html"
history Visited pages history.back()
navigator Browser information navigator.language
localStorage Saved data localStorage.setItem("k", "v")

Example 4.2: Objects and built-in objects

Problem: Create a student object with a method for the grade, a class Course, and use Math, Number and JSON built-in objects.

Solution:

  1. Write an object literal with name, marks and a grade() method.
  2. Write class Course with a constructor and a describe() method.
  3. Use Math.max, toFixed and JSON.stringify.

Result: The script below prints the values produced by the objects.

const student = {
  name: "Asha", marks: [78, 92, 85],
  average() {
    return this.marks.reduce((a, b) => a + b) / this.marks.length;
  },
};
class Course {
  constructor(code, title) { this.code = code; this.title = title; }
  describe() { return `${this.code}: ${this.title}`; }
}
const c = new Course("25BCA402SEC02", "Web Technologies");
console.log(student.name, "average", student.average().toFixed(2));
console.log("best:", Math.max(...student.marks), "|", c.describe());
console.log(JSON.stringify({ name: student.name, course: c.code }));
const { name, marks } = student;
console.log(name, marks.length, "subjects");

Output

Asha average 85.00
best: 92 | 25BCA402SEC02: Web Technologies
{"name":"Asha","course":"25BCA402SEC02"}
Asha 3 subjects

Common mistakes

  • Using an arrow function as an object method that needs this.
  • Changing location.href when only part of the page should change; use the DOM or AJAX.
  • Storing passwords or private data in localStorage.

Key takeaways

  • Objects hold properties and methods; classes are templates for objects.
  • Built-in objects include Math, Date, String, Array, JSON and RegExp.
  • window, location, history and navigator control the browser.
  • HTML, CSS and JavaScript combine for interactive features.

Practice questions

  1. What is a JavaScript object?
  2. How can JavaScript go back to the previous page?
  3. Name three built-in objects of JavaScript.

Answers to the practice questions

  1. A collection of properties and methods in name-value pairs.
  2. With history.back().
  3. Math, Date and JSON (also String, Array, RegExp).

Sources

  • Official BCA syllabus Web Technologies, course code 25BCA402SEC02, Unit IV