javascript
  1. javascript-object

JavaScript Object

Syntax

var objectName = {propertyName1: propertyValue1, propertyName2: propertyValue2};

Example

var person = {name: "John", age: 25, city: "New York"};

Output

{
    name: "John",
    age: 25,
    city: "New York"
}

Explanation

JavaScript objects are containers for named values, called properties. They are similar to a dictionary or a hash map in other programming languages.

An object in JavaScript can be created using an object literal, which is a comma-separated list of name-value pairs wrapped in curly braces. The name and value are separated by a colon, and each pair is separated by a comma.

Properties can be added or modified using dot notation or square bracket notation.

Use

JavaScript objects are used to represent complex data structures and organize data. They can be used to store information about anything, from a person to a car to a website.

Objects are also used in many popular JavaScript libraries and frameworks, such as jQuery and React.

Important Points

  • Objects are created using an object literal or by using the object constructor.
  • Properties can be added or modified using dot notation or square bracket notation.
  • Objects can be nested, which means an object can contain another object.

Summary

In summary, JavaScript objects are containers for named values, called properties. They are created using an object literal and can be used for organizing and storing data. Properties can be added or modified using dot notation or square bracket notation. Objects can also be nested.

Published on: