JavaScript Properties

Properties are the most important part of any JavaScript object.

Properties are the values associated with a JavaScript object.

A JavaScript object is a collection of unordered properties.

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

Accessing Object Properties

You can access the value of a property by using its key.

1. Using dot Notation

2. Using bracket Notation

1. Using dot Notation

Here's the syntax of the dot notation.


For example,


2. Using bracket Notation

Here is the syntax of the bracket notation.



JavaScript Loop

The JavaScript statement loops through the properties of an object.


for (let variable in object) {

  // code to be executed


The block of code inside of the loop will be executed once for each property


Adding New Properties

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

Assume that the person object already exists - you can then give it new properties:


Deleting Properties

The delete keyword deletes a property from an object:

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.

The delete operator is designed to be used on object properties. It has no effect on variables or functions.

The delete operator should not be used on predefined JavaScript object properties. It can crash your application.

For Example:

const person = {

  firstName: "John",

  lastName: "Doe",

  age: 50,

  eyeColor: "blue"


delete person.age;

In second example below:

delete person["age"];


Nested Objects

Values in an object can be another object:


// nested object

const student = { 

    name: 'John', 

    age: 20,

    marks: {

        science: 70,

        math: 75



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



const myObj = {

name: "John",

age: 30,

cars: {

car1: "Ford",

car2: "BMW",

car3: "Fiat"



document.getElementById("demo").innerHTML =;


Another Examples:

// accessing property of marks object

console.log(; // 70






let p1 = "marks";

let p2 = "science";



Nested Arrays and Objects

Values in objects can be arrays, and values in arrays can be objects:

const myObj = {

  name: "John",

  age: 30,

  cars: [

    {name:"Ford", models:["Fiesta", "Focus", "Mustang"]},

    {name:"BMW", models:["320", "X3", "X5"]},

    {name:"Fiat", models:["500", "Panda"]}



To access arrays inside arrays, use a for-in loop for each array:

for (let i in {

  x += "<h1>" +[i].name + "</h1>";

  for (let j in[i].models) {

    x +=[i].models[j];




Property Attributes

All properties have a name. In addition they also have a value.

The value is one of the property's attributes.

Other attributes are: enumerable, configurable, and writable.

These attributes define how the property can be accessed (is it readable?, is it writable?)

In JavaScript, all attributes can be read, but only the value attribute can be changed (and only if the property is writable).


Prototype Properties

JavaScript objects inherit the properties of their prototype.

The delete keyword does not delete inherited properties, but if you delete a prototype property, it will affect all objects inherited from the prototype.