Objects in JavaScript

prasenjit basuprasenjit basu
3 min read

Today, in this blog we will discuss about Objects, a built-in data structure in JavaScript. Objects come in two forms: the declarative (literal) form and the constructed form. Literal Syntax-> let obj = {key:value}. Constructed Syntax--> let myObj = new Object(); myBob.key = value. The constructed form and the literal form result in the same sort of objects. The difference is the literal form allows us to define one or more key-value pairs at once, while in the constructed form, we must add properties one by one. Since the literal form is widely used in the industry, we will focus on it in this discussion. In object literals, data is stored in the form of key-value pairs. Objects can hold various primitive data types, including String, Number, Boolean, Null, Undefined, as well as Arrays and Functions.

Objects play a crucial role in solving real-world problems in programming. As software developers, our primary responsibility is to solve real-life challenges, and objects help us achieve that efficiently. They allow us to represent real-world entities in our code in a structured and manageable way. Lets take an example of a real life entity which we use in our day to day life.

From the above example, you can see how easily we can represent a real-life entity in our code with the help of objects.

Accessing Object Properties

There are two common ways to access object properties:

  1. Dot Notation:- Dot notation is the most commonly used method, providing a straightforward way to retrieve values.

Bracket Notation:- Bracket notation is useful when dealing with property names that contain spaces or special characters.

JavaScript provides several useful methods for working with objects

  1. in:- The in operator, also known as the membership operator, checks whether a property exists in an object and returns true or false.

  2. Keys: The Keys method is used to get an array of keys from the object.

  3. Values: The Values method is used to get an array of values from the object.

  4. Entries: The Entries method returns an array of key-value pairs from the given object.

  5. hasOwnProperty: The hasOwnProperty method is used to check if an object has a specific property as its own, rather than inheriting it from its prototype chain. It returns a Boolean value: true if the object has the property, or false otherwise.

    JavaScript forIn Loop how works with objects

    forIn:- The for...in loop in JavaScript is used to iterate over the enumerable properties of an object. It allows us to access each key within an object and retrieve its corresponding value. During each iteration, the loop assigns a property name to a variable, which can then be used to access the value using bracket notation. However, it is important to note that for...in also iterates over inherited properties from the prototype chain. To avoid this, we can use the hasOwnProperty() method to filter out inherited properties.

Conclusion

That’s all for today’s blog. We will discuss more about object methods and memory in future blogs, so stay tuned. Also, understanding objects and their methods is essential for writing efficient JavaScript code. Mastering these concepts will help developers build scalable, structured, and maintainable applications while solving real-world problems effectively. Thank you so much. Happy Coding!

Hitesh Choudhary

0
Subscribe to my newsletter

Read articles from prasenjit basu directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

prasenjit basu
prasenjit basu

Hi, I am Prasenjit Basu Roy I am a Web-Developer.