Object
In JavaScript, an object is a complex data type that can hold various values and functions, which are called properties and methods respectively. Objects are used to store and organize data in a structured way, and they are an essential part of the language.
An object in JavaScript is created by defining a variable and assigning it to an object literal using curly braces {}. Here's an example:
In the example above, we have created an object called person
that has four properties: name
, age
, occupation
, and greet
. The first three properties are simple string and number values, while greet
is a function.
We can access and modify the values of an object's properties using dot notation or bracket notation. Here are some examples:
We can also add new properties and methods to an object at any time using dot notation or bracket notation:
Objects can also be used to create more complex data structures, such as arrays of objects, objects within objects, and so on. Here's an example of an array of objects:
In this example, we have created an array called people
that contains three objects, each with its own set of properties. We can access the properties of each object using dot notation and bracket notation as before, but we can also access the objects themselves using array notation.
Last updated