DEV Community

Jin Choo
Jin Choo

Posted on • Edited on

2 1

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.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay