MulaiCode
JavaScript

JS Object Properties

Pelajari cara mengakses dan memodifikasi properti dalam objek JavaScript.

JavaScript Object Properties

Objek di JavaScript terdiri dari properti dalam format key: value. Kita bisa mengakses dan memodifikasi properti dengan dua cara: menggunakan dot notation dan bracket notation.


Mengakses Properti

const person = {
  firstName: "Budi",
  lastName: "Santoso",
  age: 30,
};
 
console.log(person.firstName); // Dot notation
console.log(person["age"]);    // Bracket notation

Menambahkan Properti

const person = {
  name: "Andi"
};
 
person.age = 25;
 
console.log(person.age);

Menghapus Properti

const car = {
  brand: "Toyota",
  year: 2022
};
 
delete car.year;
 
console.log(car.year); // undefined

Mengecek Apakah Properti Ada

const user = {
  username: "admin"
};
 
console.log("username" in user);
console.log("email" in user);

On this page