Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST

Basic JavaScript

JS Tutorial JS Syntax JS Variables JS Operators JS If Conditions JS Loops JS Strings JS Numbers JS Functions JS Objects JS Scope JS Dates JS Temporal Dates JS Arrays JS Sets JS Maps JS Iterations JS Math JS RegExp JS Data Types JS Errors JS Debugging JS Conventions JS References JS ECMAScript 2026 JS Versions

JS HTML

JS HTML DOM JS Events JS Projects

JS Advanced

JS Functions JS Objects JS Classes JS Async JS Modules JS Meta & Proxy JS Typed Arrays JS DOM Navigation JS Windows JS Web APIs JS AJAX JS JSON JS jQuery JS Graphics JS Examples JS Reference


JavaScript Object Properties

Properties are key:value Pairs

A JavaScript object is a collection of properties

Properties can be changed, added, and deleted.

Accessing JavaScript Properties

You can access object properties in these ways:

  • Dot notation
  • Bracket notation
  • Expression

Examples

// objectName.property
let age = person.age;
//objectName["property"]
let age = person["age"];
//objectName[expression]
let age = person[x];

Dot Notation

objectName.propertyName
person.firstname + " is " + person.age;
Try it Yourself »

Bracket Notation

objectName["propertyName"]
person["firstname"] + " is " + person["age"];
Try it Yourself »

Note

In general, dot notation is preferred for readability and simplicity.

Bracket notation is necessary in some cases:

  • The property name is stored in a variable:
    person[myVariable]
  • The property name is not a valid identifier:
    person["last-name"]

Bracket notation is useful when the property name is stored in a variable:

Example

let n1 = "firstName";
let n2 = "lastName";

let name = person[n2] + " " + person[n2];
Try it Yourself »

Changing Properties

You can change the value of a property:

Example

person.age = 10;
Try it Yourself »

Adding New Properties

You can add a new property by simply giving it a value:

Example

person.nationality = "English";
Try it Yourself »


Deleting Properties

The delete keyword deletes a property from an object:

Examples

const person = {
  firstName: "John",
  lastName: "Doe",
  age: 50,
};

delete person.age;
Try it Yourself »
const person = {
  firstName: "John",
  lastName: "Doe",
  age: 50,
};

delete person["age"];
Try it Yourself »

Note:

The delete keyword deletes both the value and the property.

After deleting, the property is removed. Accessing it will return undefined.


Check if a Property Exists

Use the in operator to check if a property exists in an object:

Example

const person = {
  firstName: "John",
  lastName: "Doe"
};

let result = ("firstName" in person);
Try it Yourself »

Nested Objects

Property values in an object can be other objects:

Example

myObj = {
  name:"John",
  age:30,
  myCars: {
    car1:"Ford",
    car2:"BMW",
    car3:"Fiat"
  }
}

You can access nested objects using the dot notation or the bracket notation:

Examples

myObj.myCars.car2;
Try it Yourself »
myObj.myCars["car2"];
Try it Yourself »
myObj["myCars"]["car2"];
Try it Yourself »
let p1 = "myCars";
let p2 = "car2";
myObj[p1][p2];
Try it Yourself »

Summary

  • Object properties are key:value pairs
  • Access properties with dot notation or bracket notation
  • Add, change, and delete properties using assignment and delete
  • Use property in object to check if a property exists



×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.

-->