JavaScript Object Syntax
- Objects are composite data types that are built from primitive and other objects.
- An object’s building blocks are commonly referred to as its properties.
- Properties are used to describe some aspect of an object.
- For example, a property can describe the color of a horse.
Creating Objects
- The language provides syntax known as object literal notation for quickly creating objects.
- Object literals are denoted by curly braces.
For example:
- var object = {};
- Inside of the curly braces, properties and their values are specified as a list of key/value pairs.
- The following example creates an object with three properties using literal notation.
- The first property,abhi, holds the number ten.
- The second property, jeet, is specified using a string, and also stores a string value.
- The third property, bittoo, stores an empty object.
- var object = { abhi: 10,
- "jeet": "hello world..",
- bittoo: {}
- };
Accessing Properties
- JavaScript provides two notations for accessing object properties.
- Dot Notation
- Bracket Notation
- The first, and most common, is known as dot notation.
- Under dot notation, a property is accessed by giving the host object’s name, followed by a period (or dot), followed by the property name.
- If object.abhi initially held the value ten, then its value would become eleven after executing this statement.
- Note that if an object.abhi did not already have a value, then it would be undefined.
- object.abhi = object.abhi+ 1;
- The alternate syntax for accessing object properties is known as bracket notation.
- In bracket notation, the object name is followed by a set of square brackets. Inside the square brackets, the property name is specified as a string.For example
- object["abhi"] = object["abhi"] + 1;

Join the conversation! Your thoughts help the community grow.