JavaScript Object.keys() Method Example

The Object.keys() method returns an array of a given object's own property names, in the same order as we get with a normal loop.

Example 1 - Using Object.keys() Method

 // using Object Literals
 var user = {
    firstName : 'Ramesh',
    lastName : 'Fadatare',
    emailId : '[email protected]',
    age : 29,
    getFullName : function (){
        return user.firstName + " " + user.lastName;

function testNumberOfProperties(){
    let count = Object.keys(user).length;
    console.log("Number of properties in User object is : " + count);

Number of properties in User object is : 5
For the best learning experience, I highly recommended that you open a console (which, in Chrome and Firefox, can be done by pressing Ctrl+Shift+I), navigate to the "console" tab, copy-and-paste each JavaScript code example from this guide, and run it by pressing the Enter/Return key.
You can refer below screenshot for your testing:

Example 2

One use of the Object.keys function is to create a copy of an object that has all the properties of it, except one:
const car = {
  color: 'blue',
  brand: 'Ford'
const prop = 'color'

function createNewObject(){
    const newCar = Object.keys(car).reduce((object, key) => {
        if (key !== prop) {
          object[key] = car[key]
        return object
      }, {})

{brand: "Ford"}