Object Creation Techniques
Method 1 – Object Literal
Best for beginners.
let student = {
name: "Amit",
marks: 80,
getResult() {
return this.marks >= 40 ? "Pass" : "Fail";
}
};
console.log(student.getResult());
Method 2 – Using new Object()
let car = new Object();
car.brand = "Toyota";
car.year = 2024;
console.log(car.brand);
Method 3 – Constructor Function
function Student(name, marks) {
this.name = name;
this.marks = marks;
this.getResult = function () {
return this.marks >= 40 ? "Pass" : "Fail";
};
}
let s1 = new Student("Neha", 90);
console.log(s1.getResult());
Method 4 – ES6 Class (Modern & Professional)
class Student {
constructor(name, marks) {
this.name = name;
this.marks = marks;
}
getResult() {
return this.marks >= 40 ? "Pass" : "Fail";
}
}
let s1 = new Student("Riya", 85);
console.log(s1.getResult());
Practice Programs on JavaScript Objects
Program 1 – Student Result System
const student = {
name: "Amit",
marks: [78, 85, 90, 66, 88],
total() {
return this.marks.reduce((sum, m) => sum + m, 0);
},
average() {
return this.total() / this.marks.length;
},
grade() {
const avg = this.average();
if (avg >= 90) return "A+";
if (avg >= 75) return "A";
if (avg >= 60) return "B";
return "C";
}
};
console.log(student.name, student.grade());
Program 2 – Simple Bank Account
const account = {
holder: "Riya",
balance: 5000,
deposit(a) { this.balance += a; },
withdraw(a) { this.balance -= a; },
show() { console.log("Balance:", this.balance); }
};
account.deposit(2000);
account.withdraw(500);
account.show();
Program 3 – Employee with Salary Parts
const employee = {
name: "Rahul",
salary: { basic: 30000, hra: 8000, bonus: 2000 },
total() {
return this.salary.basic + this.salary.hra + this.salary.bonus;
}
};
console.log(employee.total());
Program 4 – Shopping Cart
const cart = {
items: [
{ name: "Book", price: 300, qty: 2 },
{ name: "Pen", price: 20, qty: 5 }
],
total() {
return this.items.reduce((t, i) => t + i.price * i.qty, 0);
}
};
console.log(cart.total());
Program 5 – Attendance Tracker
const classRoom = {
students: [
{ name: "A", present: true },
{ name: "B", present: false }
],
presentCount() {
return this.students.filter(s => s.present).length;
}
};
console.log(classRoom.presentCount());
Program 6 – Mobile Price After Discount
const mobile = {
brand: "Samsung",
price: 20000,
discount: 10,
final() {
return this.price - this.price * this.discount / 100;
}
};
console.log(mobile.final());
Program 7 – Library Object
const library = {
books: ["JS", "Python"],
add(book) { this.books.push(book); },
list() { this.books.forEach(b => console.log(b)); }
};
library.add("Java");
library.list();
Program 8 – Movie Rating
const movie = {
title: "Action Film",
ratings: [4, 5, 3, 4],
average() {
return this.ratings.reduce((a, b) => a + b) / this.ratings.length;
}
};
console.log(movie.average());
Program 9 – Weather Report
const weather = {
city: "Delhi",
temp: 30,
unit: "C",
report() {
return `${this.city}: ${this.temp}°${this.unit}`;
}
};
console.log(weather.report());
Program 10 – To-Do Manager
const todo = {
tasks: [],
add(task) { this.tasks.push(task); },
show() { console.log(this.tasks); }
};
todo.add("Study");
todo.add("Practice");
todo.show();
Program 11 – Nested User Profile
const user = {
name: "Amit",
address: { city: "Delhi", pin: 110045 }
};
console.log(user.address.pin);
Program 12 – Batch with Students
const batch = {
trainer: "Mr. X",
students: ["A", "B", "C"]
};
console.log(batch.students.join(", "));
Program 13 – Report Card
const report = {
name: "Neha",
subjects: [
{ sub: "Math", marks: 90 },
{ sub: "Sci", marks: 80 }
],
total() {
return this.subjects.reduce((t, s) => t + s.marks, 0);
}
};
console.log(report.total());
Program 14 – Inventory Check
const store = {
products: [
{ name: "Mouse", qty: 5 },
{ name: "Keyboard", qty: 3 }
],
check(name) {
return this.products.find(p => p.name === name)?.qty || 0;
}
};
console.log(store.check("Mouse"));
Program 15 – Clone & Modify
const original = { name: "Amit", age: 20 };
const copy = { ...original };
copy.age = 25;
console.log(original, copy);
Program 16 – Student Constructor
function Student(name, marks) {
this.name = name;
this.marks = marks;
this.result = function () {
return this.marks >= 40 ? "Pass" : "Fail";
};
}
const s1 = new Student("Amit", 70);
console.log(s1.result());
Program 17 – Product Constructor
function Product(name, price) {
this.name = name;
this.price = price;
}
const p = new Product("Laptop", 50000);
console.log(p.name);
Program 18 – Bank Constructor
function Bank(balance) {
this.balance = balance;
this.deposit = amt => this.balance += amt;
}
const b = new Bank(1000);
console.log(b.deposit(500));
Program 19 – Course Constructor
function Course(title, duration) {
this.title = title;
this.duration = duration;
}
const c = new Course("Web Dev", "6 months");
console.log(c.title);
Program 20 – Cart Item Constructor
function Item(name, price, qty) {
this.name = name;
this.price = price;
this.qty = qty;
this.total = () => this.price * this.qty;
}
const i = new Item("Pen", 10, 5);
console.log(i.total());
Program 21 – Car Class
class Car {
constructor(brand) {
this.brand = brand;
}
}
const car1 = new Car("Toyota");
console.log(car1.brand);
Program 22 – Employee Class
class Employee {
constructor(name, salary) {
this.name = name;
this.salary = salary;
}
annual() {
return this.salary * 12;
}
}
const e = new Employee("Ravi", 30000);
console.log(e.annual());
Program 23 – Login System
class UserLogin {
constructor(user, pass) {
this.user = user;
this.pass = pass;
}
login(p) {
console.log(p === this.pass ? "Success" : "Wrong");
}
}
const u = new UserLogin("admin", "123");
u.login("123");
Program 24 – Hotel Booking
class Hotel {
constructor(rooms) {
this.rooms = rooms;
}
book() {
if (this.rooms > 0) this.rooms--;
}
}
const h = new Hotel(2);
h.book();
console.log(h.rooms);
Program 25 – Flight Booking
class Flight {
constructor(seats) {
this.seats = seats;
}
reserve() {
if (this.seats > 0) this.seats--;
}
}
const f = new Flight(5);
f.reserve();
console.log(f.seats);
Program 26 – Order System
class Order {
constructor() {
this.items = [];
}
add(name, price, qty) {
this.items.push({ name, price, qty });
}
total() {
return this.items.reduce((t, i) => t + i.price * i.qty, 0);
}
}
const o = new Order();
o.add("Phone", 20000, 1);
console.log(o.total());
Program 27 – Wallet
class Wallet {
constructor(balance) {
this.balance = balance;
}
pay(amount) {
if (amount <= this.balance) this.balance -= amount;
}
}
const w = new Wallet(1000);
w.pay(200);
console.log(w.balance);
Program 28 – Quiz System
class Quiz {
constructor() {
this.score = 0;
}
correct() { this.score++; }
}
const q = new Quiz();
q.correct();
console.log(q.score);
Program 29 – Membership Validity
class Member {
constructor(expiry) {
this.expiry = new Date(expiry);
}
valid() {
return this.expiry > new Date();
}
}
const m = new Member("2026-12-31");
console.log(m.valid());
Program 30 – Parking Lot
class Parking {
constructor(spaces) {
this.spaces = spaces;
}
park() {
if (this.spaces > 0) this.spaces--;
}
}
const p = new Parking(3);
p.park();
console.log(p.spaces);