English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JavaScript Basic Tutorial

JavaScript Objects

JavaScript Functions

JS HTML DOM

JS Browser BOM

AJAX Basic Tutorial

JavaScript Reference Manual

JavaScript Object Properties

Propertiesare the associations between names (keys) and values within an object, and they can contain any data type.

PropertiesThey usually refer to the characteristics of an object.

They can usually be changed, added, and deleted, but some properties are read-only.

Access JavaScript properties

There are two ways to access object properties:

  • Dot notation: .

  • Bracket notation: []

Let's revisit the original example objectuser.

user.firstName;
Test and see‹/›
user["firstName"];
Test and see‹/›

The dot and bracket symbols are often used. However, the (.) dot notation is faster and easier to read.

Add new property

To add a new property to an object, you can use the assignment operator (=) to assign a new value to the property.

Assuming the user object already exists-Then you can assign new properties to it:

user.weapon = "Sword";
Test and see‹/›

Note: Avoid using reserved words for property or method names.

Modify object property

Object properties can be modified by assigning a new value to an existing property.

var user = {firstName:"Seagull", lastName:"Anna", age:22, location:"New Delhi"};
user.location = "Goa";
Test and see‹/›

Delete object property

To delete a property from an object, we will use the delete keyword.

Delete is the operator used to delete properties from an object, which can delete an object's properties.

The following example demonstrates how to delete a property from an object:

var user = {firstName:"Seagull", lastName:"Anna", age:22, location:"New Delhi"};
delete user.location;
Test and see‹/›

The delete operator will remove the two values of the property and the property itself.

Do not use the delete operator on predefined JavaScript object properties. It may cause your application to crash.

traversing object properties

JavaScript hasfor...inSpecifically used for iterating over object properties.

Syntax:

for (variable in object) { statement to be executed }

This is the simplified version of our main object example: the user:

var user = {firstName:"Seagull", lastName:"Anna", age:22, location:"New Delhi"};
for (let x in user) {
    document.write(x);
}
Test and see‹/›

Using parentheses, we can retrieve the property value as a variable, in this example as x:

for (let x in user) {
document.write(user[x]);
}
Test and see‹/›

for...inThe loop should not be confused withfor...ofLoop confusion, the latter is only used on Array object types.

Another useful enumeration method is the Object.keys() method, which returns an array of object properties.

// Initialize methods on the user object to return property keys
Object.keys(user);
Test and see‹/›

This method allows us to use the properties of the object as an array, so you can take advantage of all the methods of JavaScript arrays.

Internal Properties of Properties

All properties have a name. In addition, they also have property values.

This value is one of the properties of the property.

Other properties are: enumerable, configurable, and writable.

These properties define the access mode of the properties (readability, writability).

In JavaScript, all properties can be read, but property values can only be changed (and only when the property is in a writable state).