Preliminaries

JavaScript has only one data type which can contain multiple values: Object. An Array is a special form of object.

(Plain) Objects have the form

{key: value, key: value, ...}

Arrays have the form

[value, value, ...]

Both arrays and objects expose a key -> value structure. Keys in an array must be numeric, whereas any string can be used as key in objects. The key-value pairs are also called the "properties".

Properties can be accessed either using dot notation

const value = obj.someProperty;

or bracket notation, if the property name would not be a valid JavaScript identifier name [spec], or the name is the value of a variable:

// the space is not a valid character in identifier names
const value = obj["some Property"];

// property name as variable
const name = "some Property";
const value = obj[name];

For that reason, array elements can only be accessed using bracket notation:

const value = arr[5]; // arr.5 would be a syntax error

// property name / index as variable
const x = 5;
const value = arr[x];

Wait... what about JSON?

JSON is a textual representation of data, just like XML, YAML, CSV, and others. To work with such data, it first has to be converted to JavaScript data types, i.e. arrays and objects (and how to work with those was just explained). How to parse JSON is explained in the question Parse JSON in JavaScript? .

Further reading material

How to access arrays and objects is fundamental JavaScript knowledge and therefore it is advisable to read the MDN JavaScript Guide, especially the sections

  • Working with Objects
  • Arrays
  • Eloquent JavaScript - Data Structures


Accessing nested data structures

A nested data structure is an array or object which refers to other arrays or objects, i.e. its values are arrays or objects. Such structures can be accessed by consecutively applying dot or bracket notation.

Here is an example:

const data = {
    code: 42,
    items: [{
        id: 1,
        name: 'foo'
    }, {
        id: 2,
        name: 'bar'
    }]
};

Let's assume we want to access the name of the second item.

Here is how we can do it step-by-step:

As we can see data is an object, hence we can access its properties using dot notation. The items property is accessed as follows:

data.items

The value is an array, to access its second element, we have to use bracket notation:

data.items[1]

This value is an object and we use dot notation again to access the name property. So we eventually get:

const item_name = data.items[1].name;

Alternatively, we could have used bracket notation for any of the properties, especially if the name contained characters that would have made it invalid for dot notation usage:

const item_name = data['items'][1]['name'];

I'm trying to access a property but I get only undefined back?

Most of the time when you are getting undefined, the object/array simply doesn't have a property with that name.

const foo = {bar: {baz: 42}};
console.log(foo.baz); // undefined

Use console.log or console.dir and inspect the structure of object / array. The property you are trying to access might be actually defined on a nested object / array.

console.log(foo.bar.baz); // 42

What if the property names are dynamic and I don't know them beforehand?

If the property names are unknown or we want to access all properties of an object / elements of an array, we can use the for...in [MDN] loop for objects and the for [MDN] loop for arrays to iterate over all properties / elements.

Objects

To iterate over all properties of data, we can iterate over the object like so:

for (const prop in data) {
    // `prop` contains the name of each property, i.e. `'code'` or `'items'`
    // consequently, `data[prop]` refers to the value of each property, i.e.
    // either `42` or the array
}

Depending on where the object comes from (and what you want to do), you might have to test in each iteration whether the property is really a property of the object, or it is an inherited property. You can do this with Object#hasOwnProperty [MDN].

As alternative to for...in with hasOwnProperty, you can use Object.keys [MDN] to get an array of property names:

Object.keys(data).forEach(function(prop) {
  // `prop` is the property name
  // `data[prop]` is the property value
});

Arrays

To iterate over all elements of the data.items array, we use a for loop:

for(let i = 0, l = data.items.length; i < l; i++) {
    // `i` will take on the values `0`, `1`, `2`,..., i.e. in each iteration
    // we can access the next element in the array with `data.items[i]`, example:
    // 
    // var obj = data.items[i];
    // 
    // Since each element is an object (in our example),
    // we can now access the objects properties with `obj.id` and `obj.name`. 
    // We could also use `data.items[i].id`.
}

One could also use for...in to iterate over arrays, but there are reasons why this should be avoided: Why is 'for(var item in list)' with arrays considered bad practice in JavaScript?.

With the increasing browser support of ECMAScript 5, the array method forEach [MDN] becomes an interesting alternative as well:

data.items.forEach(function(value, index, array) {
    // The callback is executed for each element in the array.
    // `value` is the element itself (equivalent to `array[index]`)
    // `index` will be the index of the element in the array
    // `array` is a reference to the array itself (i.e. `data.items` in this case)
}); 

In environments supporting ES2015 (ES6), you can also use the for...of [MDN] loop, which not only works for arrays, but for any iterable:

for (const item of data.items) {
   // `item` is the array element, **not** the index
}

In each iteration, for...of directly gives us the next element of the iterable, there is no "index" to access or use.


What if the "depth" of the data structure is unknown to me?

In addition to unknown keys, the "depth" of the data structure (i.e. how many nested objects) it has, might be unknown as well. How to access deeply nested properties usually depends on the exact data structure.

But if the data structure contains repeating patterns, e.g. the representation of a binary tree, the solution typically includes to recursively [Wikipedia] access each level of the data structure.

Here is an example to get the first leaf node of a binary tree:

function getLeaf(node) {
    if (node.leftChild) {
        return getLeaf(node.leftChild); // <- recursive call
    }
    else if (node.rightChild) {
        return getLeaf(node.rightChild); // <- recursive call
    }
    else { // node must be a leaf node
        return node;
    }
}

const first_leaf = getLeaf(root);

Show code snippet

const root = {
    leftChild: {
        leftChild: {
            leftChild: null,
            rightChild: null,
            data: 42
        },
        rightChild: {
            leftChild: null,
            rightChild: null,
            data: 5
        }
    },
    rightChild: {
        leftChild: {
            leftChild: null,
            rightChild: null,
            data: 6
        },
        rightChild: {
            leftChild: null,
            rightChild: null,
            data: 7
        }
    }
};
function getLeaf(node) {
    if (node.leftChild) {
        return getLeaf(node.leftChild);
    } else if (node.rightChild) {
        return getLeaf(node.rightChild);
    } else { // node must be a leaf node
        return node;
    }
}

console.log(getLeaf(root).data);
Run code snippetEdit code snippet
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-access-array-of-objects-in-javascript
How to Access Array of Objects in JavaScript ? - GeeksforGeeks
The filter() method in JavaScript is used to access and create a new array of objects that meet specific criteria.
Published: July 23, 2025
🌐
freeCodeCamp
freecodecamp.org › news › how-to-access-properties-from-an-array-of-objects-in-javascript
How to Access Properties from an Array of Objects in JavaScript
February 29, 2024 - If we are looking for a specific object from an array of objects, we can use the find method. The find method returns the first element in the array that satisfies the provided testing function.
Discussions

javascript - Access a specific object from an array of objects - Stack Overflow
I have a variable which returns an object. This object has multiple properties, the first property is an array of more objects. I want to access this one by one using javascript and render it using... More on stackoverflow.com
🌐 stackoverflow.com
How to properly access objects in Array?
donuts.forEach(({ type, cost }) => console.log(`${type} donuts cost $${cost} each`)); https://jsfiddle.net/2r4cjLxj/ or if you can't destructure: donuts.forEach((donut) => console.log(`${donut.type} donuts cost $${donut.cost} each`)); More on reddit.com
🌐 r/javascript
9
0
May 7, 2018
jquery - Access array of objects in Javascript - Stack Overflow
How can I access array of objects ( and properties ) in Javascript? I have an array of Users (object properties : userID , fName , lName ) in my action and want to show lName of users in auto compl... More on stackoverflow.com
🌐 stackoverflow.com
Javascript: How to access an array object? - Stack Overflow
If you want to do operations on an object that is printed to the chrome console, right click on the object and do Store as global variable. Then you can access it using the name temp1 or whatever is printed out in the console. ... You can access array values by entering index of theme. More on stackoverflow.com
🌐 stackoverflow.com
Top answer
1 of 16
1474

Preliminaries

JavaScript has only one data type which can contain multiple values: Object. An Array is a special form of object.

(Plain) Objects have the form

{key: value, key: value, ...}

Arrays have the form

[value, value, ...]

Both arrays and objects expose a key -> value structure. Keys in an array must be numeric, whereas any string can be used as key in objects. The key-value pairs are also called the "properties".

Properties can be accessed either using dot notation

const value = obj.someProperty;

or bracket notation, if the property name would not be a valid JavaScript identifier name [spec], or the name is the value of a variable:

// the space is not a valid character in identifier names
const value = obj["some Property"];

// property name as variable
const name = "some Property";
const value = obj[name];

For that reason, array elements can only be accessed using bracket notation:

const value = arr[5]; // arr.5 would be a syntax error

// property name / index as variable
const x = 5;
const value = arr[x];

Wait... what about JSON?

JSON is a textual representation of data, just like XML, YAML, CSV, and others. To work with such data, it first has to be converted to JavaScript data types, i.e. arrays and objects (and how to work with those was just explained). How to parse JSON is explained in the question Parse JSON in JavaScript? .

Further reading material

How to access arrays and objects is fundamental JavaScript knowledge and therefore it is advisable to read the MDN JavaScript Guide, especially the sections

  • Working with Objects
  • Arrays
  • Eloquent JavaScript - Data Structures


Accessing nested data structures

A nested data structure is an array or object which refers to other arrays or objects, i.e. its values are arrays or objects. Such structures can be accessed by consecutively applying dot or bracket notation.

Here is an example:

const data = {
    code: 42,
    items: [{
        id: 1,
        name: 'foo'
    }, {
        id: 2,
        name: 'bar'
    }]
};

Let's assume we want to access the name of the second item.

Here is how we can do it step-by-step:

As we can see data is an object, hence we can access its properties using dot notation. The items property is accessed as follows:

data.items

The value is an array, to access its second element, we have to use bracket notation:

data.items[1]

This value is an object and we use dot notation again to access the name property. So we eventually get:

const item_name = data.items[1].name;

Alternatively, we could have used bracket notation for any of the properties, especially if the name contained characters that would have made it invalid for dot notation usage:

const item_name = data['items'][1]['name'];

I'm trying to access a property but I get only undefined back?

Most of the time when you are getting undefined, the object/array simply doesn't have a property with that name.

const foo = {bar: {baz: 42}};
console.log(foo.baz); // undefined

Use console.log or console.dir and inspect the structure of object / array. The property you are trying to access might be actually defined on a nested object / array.

console.log(foo.bar.baz); // 42

What if the property names are dynamic and I don't know them beforehand?

If the property names are unknown or we want to access all properties of an object / elements of an array, we can use the for...in [MDN] loop for objects and the for [MDN] loop for arrays to iterate over all properties / elements.

Objects

To iterate over all properties of data, we can iterate over the object like so:

for (const prop in data) {
    // `prop` contains the name of each property, i.e. `'code'` or `'items'`
    // consequently, `data[prop]` refers to the value of each property, i.e.
    // either `42` or the array
}

Depending on where the object comes from (and what you want to do), you might have to test in each iteration whether the property is really a property of the object, or it is an inherited property. You can do this with Object#hasOwnProperty [MDN].

As alternative to for...in with hasOwnProperty, you can use Object.keys [MDN] to get an array of property names:

Object.keys(data).forEach(function(prop) {
  // `prop` is the property name
  // `data[prop]` is the property value
});

Arrays

To iterate over all elements of the data.items array, we use a for loop:

for(let i = 0, l = data.items.length; i < l; i++) {
    // `i` will take on the values `0`, `1`, `2`,..., i.e. in each iteration
    // we can access the next element in the array with `data.items[i]`, example:
    // 
    // var obj = data.items[i];
    // 
    // Since each element is an object (in our example),
    // we can now access the objects properties with `obj.id` and `obj.name`. 
    // We could also use `data.items[i].id`.
}

One could also use for...in to iterate over arrays, but there are reasons why this should be avoided: Why is 'for(var item in list)' with arrays considered bad practice in JavaScript?.

With the increasing browser support of ECMAScript 5, the array method forEach [MDN] becomes an interesting alternative as well:

data.items.forEach(function(value, index, array) {
    // The callback is executed for each element in the array.
    // `value` is the element itself (equivalent to `array[index]`)
    // `index` will be the index of the element in the array
    // `array` is a reference to the array itself (i.e. `data.items` in this case)
}); 

In environments supporting ES2015 (ES6), you can also use the for...of [MDN] loop, which not only works for arrays, but for any iterable:

for (const item of data.items) {
   // `item` is the array element, **not** the index
}

In each iteration, for...of directly gives us the next element of the iterable, there is no "index" to access or use.


What if the "depth" of the data structure is unknown to me?

In addition to unknown keys, the "depth" of the data structure (i.e. how many nested objects) it has, might be unknown as well. How to access deeply nested properties usually depends on the exact data structure.

But if the data structure contains repeating patterns, e.g. the representation of a binary tree, the solution typically includes to recursively [Wikipedia] access each level of the data structure.

Here is an example to get the first leaf node of a binary tree:

function getLeaf(node) {
    if (node.leftChild) {
        return getLeaf(node.leftChild); // <- recursive call
    }
    else if (node.rightChild) {
        return getLeaf(node.rightChild); // <- recursive call
    }
    else { // node must be a leaf node
        return node;
    }
}

const first_leaf = getLeaf(root);

Show code snippet

const root = {
    leftChild: {
        leftChild: {
            leftChild: null,
            rightChild: null,
            data: 42
        },
        rightChild: {
            leftChild: null,
            rightChild: null,
            data: 5
        }
    },
    rightChild: {
        leftChild: {
            leftChild: null,
            rightChild: null,
            data: 6
        },
        rightChild: {
            leftChild: null,
            rightChild: null,
            data: 7
        }
    }
};
function getLeaf(node) {
    if (node.leftChild) {
        return getLeaf(node.leftChild);
    } else if (node.rightChild) {
        return getLeaf(node.rightChild);
    } else { // node must be a leaf node
        return node;
    }
}

console.log(getLeaf(root).data);
Run code snippetEdit code snippet
2 of 16
108

You can access it this way

data.items[1].name

or

data["items"][1]["name"]

Both ways are equal.

🌐
TutorialsPoint
tutorialspoint.com › How-to-access-properties-of-an-array-of-objects-in-JavaScript
How to access properties of an array of objects in JavaScript?
The property in the "object. property" syntax must be a valid JavaScript identifier. The expression should return an object, with the identifier being the name of the property you want to access. In a chain, you may use the dot property accessor to get to deeper properties such as ...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-loop-through-an-array-containing-multiple-objects-and-access-their-properties-in-javascript
How to Traverse Array of Objects and Access the Properties in JavaScript? - GeeksforGeeks
July 23, 2025 - The Object.entries() method in JavaScript returns an array consisting of enumerable property [key, value] pairs of the object. ... const a = [ {name: 'Saritha', sub: 'Maths'}, {name: 'Sarthak', sub: 'Science'}, {name: 'Sneha', sub: 'Social'} ...
🌐
Stack Overflow
stackoverflow.com › questions › 73690851 › access-a-specific-object-from-an-array-of-objects
javascript - Access a specific object from an array of objects - Stack Overflow
you can use 2 for loops, first one to loop through the array of objects, and second one to loop through properties. as suggested in the comments you can create an array of property names, and use for your second loop and print it easily.
🌐
Reddit
reddit.com › r/javascript › how to properly access objects in array?
r/javascript on Reddit: How to properly access objects in Array?
May 7, 2018 -

For a quiz on the Front-End Development Nanodegree I was given an array with objects inside

var donuts = [
{ type: "Jelly", cost: 1.22 },
{ type: "Chocolate", cost: 2.45 },
{ type: "Cider", cost: 1.59 },
{ type: "Boston Cream", cost: 5.99 }
];

and my job was to iterate through the objects in the array using the .forEach method. Well I basically hacked it and made an iteration variable to help me use an index to access each object.

var i = 0;
donuts.forEach(function(donutSummary) {

var donut = donuts[i].type;
var cost = donuts[i].cost;

console.log(donut + " donuts cost $" + cost + " each");
i = i + 1;
});

on the top of my code I declared and assigned a variable, i, for my index. I know there has to be a better way to access the objects in this array. Can anyone tell me what is the proper method to do this?

Thank you!

Find elsewhere
🌐
SheCodes
shecodes.io › athena › 12988-how-to-access-array-of-objects-properties-in-javascript
[JavaScript] - How to access array of objects properties in | SheCodes
Learn how to access the properties inside an array of objects in JavaScript and perform operations using if else statements.
🌐
TutorialsPoint
tutorialspoint.com › How-to-access-methods-of-an-array-of-objects-in-JavaScript
How to access methods of an array of objects in JavaScript?
<html> <body> <p> The JavaScript program to access the methods of an array of objects using the array prototype method call.
🌐
W3Schools
w3schools.com › js › js_arrays.asp
JavaScript Arrays
Objects use names to access its "members". In this example, person.firstName returns John: const person = {firstName:"John", lastName:"Doe", age:46}; Try it Yourself » · JavaScript variables can be objects. Arrays are special kinds of objects.
🌐
Medium
chrisvhur.medium.com › how-to-access-an-array-of-objects-using-typescript-or-javascript-da2eda025ba4
How to access an Array of Objects using TypeScript or JavaScript. | by Christian Hur | Medium
November 3, 2018 - Notice the curly braces — that’s the main distinction between an array and an object. The variable pets_2 is an object. Inside each pair of { } is a key:value pair called “property”. Our example has three properties named 0, 1, & 2 (not meaningful yet but just for illustration purposes). To access these properties of the pets_2 object, you can reference exactly the same way as the indexed array:
🌐
YouTube
youtube.com › watch
JavaScript Tip: Retrieving a Property from an Array of Objects
Enjoy the videos and music you love, upload original content, and share it all with friends, family, and the world on YouTube.
🌐
Stack Overflow
stackoverflow.com › questions › 16756939 › access-array-of-objects-in-javascript
jquery - Access array of objects in Javascript - Stack Overflow
<script> $(function() { var values = document.getElementById('s').value; var availableTags = values.split(","); function split( val ) { return val.split( /,\s*/ ); } function extractLast( term ) { return split( term ).pop(); } $( "#tags" ) // don't navigate away from the field on tab when selecting an item .bind( "keydown", function( event ) { if ( event.keyCode === $.ui.keyCode.TAB && $( this ).data( "ui-autocomplete" ).menu.active ) { event.preventDefault(); } }) .autocomplete({ minLength: 0, source: function( request, response ) { // delegate back to autocomplete, but extract the last term
🌐
Quora
quora.com › How-do-you-access-an-array-in-an-object-in-JS
How to access an array in an object in JS - Quora
Answer (1 of 4): [code]const numbers = [1, 2, 3, 4]; // Create an array of numbers. const first = numbers[0]; // Arrays are 0 based in JavaScript. const second = numbers["1"]; // Arrays indices are actually strings. // Append an element to the array. numbers[numbers.length] = 5; // Arrays resiz...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array
Array - JavaScript - MDN Web Docs
July 28, 2026 - Array elements are object properties in the same way that toString is a property (to be specific, however, toString() is a method). Nevertheless, trying to access an element of an array as follows throws a syntax error because the property name is not valid: ... JavaScript syntax requires properties beginning with a digit to be accessed using bracket notation instead of dot notation.
🌐
xjavascript
xjavascript.com › blog › how-to-loop-through-an-array-containing-objects-and-access-their-properties
How to Loop Through an Array of Objects in JavaScript and Access Their Properties: Fixing Undefined Errors and Common Looping Issues — xjavascript.com
Here, users is an array, and each user object has properties (some optional, like age in Charlie’s case). Looping through this array lets you access these properties to display data, filter users, or compute values (e.g., average age). JavaScript offers multiple ways to loop through arrays.
🌐
freeCodeCamp
forum.freecodecamp.org › curriculum help
Accessing an array's object's property, how? - Curriculum Help - The freeCodeCamp Forum
October 27, 2021 - The curriculum covers accessing nest objects, but it presented what they called complex objects wit han exemple of an array containing objects. Now i learned from this forums some answers about accessing object’s array’s entriee it was easy. Now the table has turned, it’s the array that is containing the object.