JavaScript: Object Properties

JavaScript: Object Properties

In this tutorial, you will learn everything about JavaScript Object Properties.

JavaScript Object Properties

JavaScript objects have properties, which are created by a value associated with their key.

The following syntax represents the js object:

let book = {

}

You can define properties of book object like:

let book = {
  title: 'javascript'
}

Here, book object with a property named title, with value javascript.

Get the value of a property

Get the value of book object property.

There are two different ways to get the value of object properties. You can see the following:

Using dot notation:

The following syntax is to get the object properties by using dot notation:

book.title //'javascript'

Using square brackets:

The following syntax is to get the object properties by using square brackets:

book['title'] //'javascript'

Note that, If you want to access an undefined property, you will get an error, see the following:

book.price //undefined

Here, The following example shows the nested object properties:

let book = {
  color: {
    name: 'Red'
  },
  title: 'javascript'
}

You can see the two syntax to represent, how to access the nested object properties:

book.color.name

or

book['color']['name']

Add and Update the value of a property

When you have define the object and want to update it properties later, you can see the following:

let book = {
  title : 'js'
}

book.title= 'javascript'

If you want to add new properties to an object, you can see the following:

book.price= '$100'

How to remove a property from object

We have added new properties name price in the book objects. Here, you will learn how to remove properties from objects.

You can use the delete keyword of javascript to a property from object, see the following:

delete book.price
or
delete book['price']

Remove a property without mutating the object

You can use reduce() and objects.keys() method of javascript to iterate the objects and you can create a new objects and add the properties as you want, you can see the following:

let book = {
  title: 'javascript',
  price: '$100'
}
let prop = 'price'

let newBook = Object.keys(book).reduce((object, key) => {
  if (key !== prop) {
    object[key] = book[key]
  }
  return object
}, {})

console.log(newBook) //{title: "javascript"}

Count properties in a JavaScript object

Now, you will learn how to count the number of properties of the javascript object.

You can use Object.keys() method, passing the object with length property to count their object properties.

See the following:

let book = {
  title: 'javascript',
  price: '$100'
}

let res = Object.keys(book).length;

console.log(res); //

Check if object exists or not

You can use the typeof operator to check if an object property exists or undefine.

The following example represents, how to check if a JavaScript object property is undefined:

let book = {
  title: 'javascript'
}

console.log(typeof book.title); // string
console.log(typeof book.price); // undefine

The typeof operator returns the type of the variable on which it is called as a string. The return string for any object that does not exist is “undefined”.

Recommended JavaScript Tutorials

Recommended:-JavaScript Arrays

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *