# How to convert an object to an array in JavaScript

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published Aug 22, 2020. Topics: JavaScript.
Canonical URL: https://attacomsian.com/blog/javascript-convert-object-to-array

> A detailed article to learn how to convert an object to an array in JavaScript.

To convert an [object](https://attacomsian.com/blog/objects-prototypes-classes-javascript) to an [array](https://attacomsian.com/blog/javascript-arrays) in JavaScript, you can use one of the following three methods: `Object.keys()`, `Object.values()`, and `Object.entries()`.

The `Object.keys()` method was introduced in ES6 or ECMAScript 2015. Later in ECMAScript 2017 (ES8), two new methods, `Object.entries()` and `Object.values()`, were added to convert an object to an array.

Let us say you have got the following `foods` object:

```javascript
const foods = {
    pizza: '?',
    burger: '?',
    fries: '?',
    cake: '?'
};
```

## `Object.keys()` Method

The `Object.keys()` method returns an array of the given object's own enumerable property names. The order of the property names is the same as you get while [iterating over the properties of the object](https://attacomsian.com/blog/javascript-iterate-objects#objectkeys-method) manually.

Here is an example that converts property's names of the `foods` object to an array:

```javascript
const keys = Object.keys(foods);

console.log(keys);
// [ 'pizza', 'burger', 'fries', 'cake' ]
```

## `Object.values()` Method

The [Object.values()](https://attacomsian.com/blog/object-entries-values-javascript#objectvalues-method) method is similar to `Object.keys()` except that it returns an array of the given object's own enumerable property values:

```javascript
const keys = Object.values(foods);

console.log(keys);
// [ '?', '?', '?', '?' ]
```

The order of the property's values is the same as that provided by the [for..in](https://attacomsian.com/blog/for-in-loop-in-javascript) loop.

## `Object.entries()` Method

The [Object.entries()](https://attacomsian.com/blog/object-entries-values-javascript#objectentries-method) method converts the own enumerable string-keyed property pairs of an object to an array:

```javascript
const keys = Object.entries(foods);

console.log(keys);
// [
//     ['pizza', '?'],
//     ['burger', '?'],
//     ['fries', '?'],
//     ['cake', '?']
// ]
```

The order of the property's key-value pairs is similar to what [for...in](https://attacomsian.com/blog/for-in-loop-in-javascript) loop provides.

## Browser compatibility

The `Object.keys()` method works in all modern browsers and IE9 and up. However, the `Object.values()` and `Object.entries()` methods are not supported by Internet Explorer and are only available to use in modern browsers.

**Read Next:** [How to convert an array to an object in JavaScript](https://attacomsian.com/blog/javascript-convert-array-to-object)
