DEV Community

Jin Choo
Jin Choo

Posted on • Updated on

What is an Object? - Part I

objects

Objects

An object is a data type that allows you to combine several variables into a single variable with keys and values. This is frequently used to represent or describe an entity.

Example:

const user = {
   id: 1,
   firstName: 'Charlie',
   age: 21
};
Enter fullscreen mode Exit fullscreen mode

Read the value of a property

Use the dot notation to read the value of a property in an object.

const user = {
   id: 1,
   firstName: 'Charlie',
   age: 21
};

user.id; // 1
user.firstName; // 'Charlie'
user.lastName; // 'undefined'(property does not exists)
Enter fullscreen mode Exit fullscreen mode

Updating Property Value

The same dot notation followed by an equal sign can be used to update a property value as well:

const user = {
   id: 1,
   firstName: 'Charlie',
   age: 21
};

user.firstName = 'Brody';
user.age = user.age + 1;
console.log(user); // {id: 1, firstName: 'Brody', age: 22}
Enter fullscreen mode Exit fullscreen mode

const does not imply that a variable is a constant; it only indicates that you cannot reassign it, therefore you are able to change the property value of an object defined by const. As a result, even if the variable's content (properties) can change, it is always an object.

Top comments (0)