What is an Object in JavaScript?

Objects in JavaScript are collections of keyed values. Think of them as a set of properties or values that describe something. In this article, we’ll explore what they are and how to work with them.

Checkout this video:

What is an object?

An object is a collection of properties, and a property is an association between a name (or key) and a value. A property’s value can be a function, in which case the property is known as a method. In addition to objects that are predefined in the browser, you can define your own objects.

Objects are sometimes called associative arrays, since each property is named with an associated string value.

Objects in JavaScript

In JavaScript, an object is a collection of properties, and a property is an association between a name (or key) and a value. A property’s value can be a function, in which case the property is known as a method. In addition to objects that are predefined in the language, you can define your own objects.

Creating objects

Just as you can create a standard JavaScript object by using an object literal, you can also create an AngularJS scope object in exactly the same way. For example, consider the following code:

var myScope = {};
myScope.name = “Ari Lerner”;
myScope.course = “AngularJS”;
console.log(myScope);
This would output the following object to the console:

Object {name: “Ari Lerner”, course: “AngularJS”}
As you can see, creating an AngularJS scope object is exactly the same as creating a standard JavaScript object. In fact, behind the scenes, AngularJS does nothing more than create a new empty JavaScript object when you create a new AngularJS scope.

Adding properties and methods to objects

In the previous section, we learned that objects can contain both properties and methods. In this section, we’ll learn how to add both properties and methods to our objects.

Adding a property to an object is pretty simple:
“`
var myObject = {};

myObject.someProperty = “Hello, world!”;
console.log(myObject.someProperty); // outputs “Hello, world!”
“`
As you can see, all we did was add a new property called someProperty to our myObject object, and set its value to the string “Hello, world!”

We can also add methods to our objects:
“`
var myObject = {};

myObject.someMethod = function () {
console.log(“Hello, world!”);
}; // note the semi-colon here!
myObject.someMethod(); // outputs “Hello, world!”;
“`

Accessing and manipulating objects

JavaScript objects can be accessed and manipulated in a number of ways. In this article, we will look at some of the most common methods.

The simplest way to access an object is by using its dot notation. For example, if we have an object called “person” with a property called “name”, we can access the name property like this:

person.name

We can also access object properties using bracket notation. This is useful if the property name is stored in a variable:

var propertyName = “name”;
person[propertyName] // This will evaluate to “name”

If we want to set a property on an object, we can do so using either dot notation or bracket notation:

person.name = “John”; // Set the “name” property to “John”
person[“name”] = “John”; // This does the same thing as above

Conclusion

As we have seen, a JavaScript object is an unordered collection of variables (called properties or keys) and functions (called methods). You can create an object manually by using the Object() constructor or the object initializer / literal syntax.

The Object class is available in all browsers, and you can use it to create objects. However, the Object class has several drawbacks:

– It is a global object, so it pollutes the global namespace.
– It is a built-in object, so it cannot be extended.
– It does not have a standard way of enumerating its properties and methods.

Therefore, it is generally better to use one of the other ways of creating objects.

Scroll to Top