Group a Student Record

Group a Student Record

An object groups named properties. A second variable assigned that object refers to the same object, so an update through either name is observable through the other.

Before this topic, review Choose a Method's Receiver if its terms are unfamiliar.

What you’ll learn

  • Explain group a student record through the school example.
  • Trace the shown operations and distinguish their result from displayed output.
  • Make the practice change and explain its consequence: Both names access the same record, whose mark is updated to 80.

Work through the school example

This is JavaScript using language features and the host console. Read the initial values, then follow each operation to its displayed result. A declaration names a value, a function call performs an operation, and console.log displays its arguments.

JavaScript · school.js · complete example

const student = { name: "Diya", marks: 70 };
const same = student;
same.marks = 72;
console.log(student.marks);

Expected output

72
Run in JavaScript compiler →

Two names, one record

JavaScript · source for this model

const student = { name: "Diya", marks: 70 };
const same = student;
same.marks = 72;
console.log(student.marks);

This diagram follows the supplied example. It is an explanatory model, not a JavaScript engine or a timing guarantee.

Two names, one record

  1. Create the record

    student refers to a record with mark 70.

    70
  2. Share access

    same refers to that same record, not a copied record.

    student → record ← same
  3. Update the shared record

    Writing same.marks changes what student.marks reads.

    72
Follow the relationships and values in the example.

Introduction to JavaScript Objects

In JavaScript, objects are everywhere. From storing user data to building APIs or handling events—objects hold key-value pairs that power the language. If you're aiming to build interactive websites or master frameworks like React or Node.js, understanding objects is non-negotiable.

What is a JavaScript Object?

A JavaScript object is a collection of properties, where each property is a key-value pair. Think of it as a dictionary or real-world entity, like a car or a person.

const car = {
  brand: "Toyota",
  model: "Corolla",
  year: 2021
};
{ brand: "Toyota", model: "Corolla", year: 2021 }

Creating Objects

1. Using Object Literals

const user = {
  name: "Alice",
  age: 25,
  isMember: true
};

2. Using the Object Constructor

const user = new Object();
user.name = "Bob";
user.age = 30;

3. Using a Function Constructor

function User(name, age) {
  this.name = name;
  this.age = age;
}
const user1 = new User("Charlie", 28);

Accessing Object Properties

You can access object properties in two ways:

console.log(user.name); // Dot notation
console.log(user["age"]); // Bracket notation

Modifying and Adding Properties

user.age = 26;       // Modify
user.city = "Paris"; // Add
{ name: "Alice", age: 26, isMember: true, city: "Paris" }

Deleting Properties

delete user.city;

Nested Objects

const person = {
  name: "David",
  contact: {
    email: "david@example.com",
    phone: "1234567890"
  }
};
console.log(person.contact.email);
"david@example.com"

Object Methods

Objects can contain functions as values called methods.

const calculator = {
  add: function(x, y) {
    return x + y;
  }
};
console.log(calculator.add(5, 3));
8

this Keyword

The this keyword refers to the object from which the method was called.

const profile = {
  username: "techie",
  showUsername: function() {
    return this.username;
  }
};
console.log(profile.showUsername());
"techie"

Looping Through Objects

Using for...in loop

for (let key in user) {
  console.log(key + ": " + user[key]);
}

Built-in Object Methods

  • Object.keys(obj) – returns all property names
  • Object.values(obj) – returns all values
  • Object.entries(obj) – returns an array of [key, value] pairs
console.log(Object.keys(user));
console.log(Object.values(user));
console.log(Object.entries(user));

Checking for Property Existence

"userId" in user; // false
user.hasOwnProperty("age"); // true

Object Destructuring

const { name, age } = user;
console.log(name, age);
"Alice", 26

Cloning and Merging Objects

1. Using Object.assign()

const original = { a: 1 };
const clone = Object.assign({}, original);

2. Using Spread Operator

const newClone = { ...original };

Freezing and Sealing Objects

  • Object.freeze(obj) – Prevents adding/removing/modifying properties
  • Object.seal(obj) – Prevents adding/removing but allows modifying existing
Object.freeze(user);
user.name = "Eve"; // Won’t work
console.log(user.name);
"Alice"

Summary

JavaScript objects are powerful and versatile. They serve as the foundation for JSON, API calls, configurations, and complex data models. Mastering object manipulation is key to advancing in any JS-based role or framework.

What's Next?

In the upcoming modules, you’ll dive into arrays, which are object-like structures perfect for ordered data. Once you pair them with objects, your JavaScript fluency takes a huge leap forward.

Recognize the important boundary

const protects the binding, not the fields. Use an intentional copy when you need independent data; spread makes only a shallow copy.

Try a small variation

JavaScript · original source for practice

const student = { name: "Diya", marks: 70 };
const same = student;
same.marks = 72;
console.log(student.marks);

Your task: Replace same.marks = 72 with same.marks = 80. Predict the result before running the edited program.

The source is repeated here. If the edit targets the HTML rather than the script, use the markup repeated below; the worked answer also repeats the changed markup.

Worked answer and reasoning

Both names access the same record, whose mark is updated to 80.

JavaScript · complete worked variation

const student = { name: "Diya", marks: 70 };
const same = student;
same.marks = 80;
console.log(student.marks);

Expected output

80
Run in JavaScript compiler →

Predict the original program

JavaScript · original program for this question

const student = { name: "Diya", marks: 70 };
const same = student;
same.marks = 72;
console.log(student.marks);
What does the original JavaScript program display, in order?
Choose one answer. You can try again.

Answer and explanation

72

An object groups named properties. A second variable assigned that object refers to the same object, so an update through either name is observable through the other. const protects the binding, not the fields. Use an intentional copy when you need independent data; spread makes only a shallow copy.

Keep the useful idea

An object groups named properties. A second variable assigned that object refers to the same object, so an update through either name is observable through the other.

Check your variation against this explanation: Both names access the same record, whose mark is updated to 80.

Use the JavaScript array reference library for individual array operations and boundary tests to check a rule independently.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Store an Ordered Class List →