Lecture 15: Objects & Scope

BMC201 - Web Technology

Mr. Prashant Kumar Nag

2026-02-18

Lecture 15

Objects & Scope

Week 4 | Unit II: JavaScript Functions & Events
BMC201 - Web Technology
Mr. Prashant Kumar Nag, Assistant Professor

Learning Objectives


  • Represent real data with JavaScript objects
  • Read, update, and add object properties
  • Define object methods
  • Understand global, function, and block scope
  • Compare var, let, and const

What Is an Object?


Objects store key-value pairs:

const student = {
  name: "Riya",
  rollNo: 42,
  isActive: true
};

Think of objects as structured records.

Accessing and Updating Properties


console.log(student.name);      // dot notation
console.log(student["rollNo"]); // bracket notation

student.city = "Noida";        // add property
student.isActive = false;       // update property

Object Methods


const user = {
  firstName: "Aarav",
  lastName: "Sharma",
  fullName() {
    return `${this.firstName} ${this.lastName}`;
  }
};

console.log(user.fullName());
  • this refers to the current object in method calls

Nested Objects and Arrays


const course = {
  code: "BMC201",
  instructor: { name: "Dr. Nag", dept: "MCA" },
  topics: ["HTML", "CSS", "JavaScript"]
};

console.log(course.instructor.name);
console.log(course.topics[2]);

Understanding Scope


Scope defines where variables are accessible:

  • Global scope
  • Function scope
  • Block scope

var, let, const Scope


var globalValue = 1;

if (true) {
  var x = 10;
  let y = 20;
  const z = 30;
}

console.log(x); // works (function/global scope)
console.log(y); // error (block scope)

Scope Chain Example


const appName = "WebTech";

function outer() {
  const unit = "JavaScript";

  function inner() {
    console.log(appName, unit);
  }

  inner();
}

outer();

Inner scopes can access outer variables.

Best Practices


  • Use const by default
  • Use let for changing values
  • Avoid var in modern code
  • Keep object names meaningful
  • Keep nested structures manageable

Questions?

Next: Lecture 16 - Arrow Functions & Callbacks