lynx   »   [go: up one dir, main page]

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 SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING 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 Dates JS Arrays JS Sets JS Maps JS Math JS RegExp JS Data Types JS Errors JS Debugging JS Events JS Programming JS References JS UTF-8 Characters JS Versions

JS Advanced

JS Functions JS Objects JS Classes JS Iterations JS Promises JS Modules JS HTML DOM JS Windows JS Web API JS AJAX JS JSON JS jQuery JS Graphics JS Examples JS Objects


JavaScript Object Properties

An Object is an Unordered Collection of Properties

Properties are the most important part of JavaScript objects.

Properties can be changed, added, deleted, and some are read only.


Accessing JavaScript Properties

The syntax for accessing the property of an object is:

// objectName.property
let age = person.age;

or

//objectName["property"]
let age = person["age"];

or

//objectName[expression]
let age = person[x];

Examples

person.firstname + " is " + person.age + " years old.";
Try it Yourself »
person["firstname"] + " is " + person["age"] + " years old.";
Try it Yourself »
let x = "firstname";
let y = "age";
person[x] + " is " + person[y] + " years old.";
Try it Yourself »

Adding New Properties

You can add new properties to an existing object by simply giving it a value:

Example

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

Property Default Values

A value given to a property will be a default value for all objects created by the constructor:

Example

function Person(first, last, age, eyecolor) {
  this.firstName = first;
  this.lastName = last;
  this.age = age;
  this.eyeColor = eyecolor;
  this.nationality = "English";
}
Try it Yourself »

Deleting Properties

The delete keyword deletes a property from an object:

Example

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

delete person.age;
Try it Yourself »

or delete person["age"];

Example

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

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

Note:

The delete keyword deletes both the value of the property and the property itself.

After deletion, the property cannot be used before it is added back again.



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 »



×

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, cookie and privacy policy.

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

Лучший частный хостинг