Object in JavaScript – What Is a JavaScript Properties Object?
An object is an element you can use to bundle multiple named values into a single item.
Syntax of a JavaScript Object
A pair of braces ({...}
) define JavaScript’s properties object.
Here’s an illustration:
The properties object in the image above contains only one property: bestColor: "pink"
.
Examples of a JavaScript Object
Let’s discuss some examples of objects in JavaScript.
Example 1: An object with two properties
Above is a properties object containing two properties named month
and year
.
We initialized the month
property with a string value while year
got assigned a number.
Example 2: An object with four properties
In the snippet above is a properties object containing four properties.
We initialized the doorNo
, tenant
, and rentPaid
properties with the number
, name
, and state
variables.
Then, we assigned a value of undefined
to the amount
property.
There are instances when JavaScript allows you to use a shorthand syntax to define properties. Let’s talk more about this below.
JavaScript Properties Shorthand Syntax
Suppose a property’s key and its assigned variable have the same name. In that case, you can use JavaScript’s shorthand notation to define the property.
For instance, consider this snippet:
You can see that the property’s key and assigned variable have the same name, thus, making the object difficult to read.
JavaScript provides a shorthand alternative to the snippet above. Let’s see it below.
You can see that the shorthand notation makes the properties object neater and easier to read.
Let’s now discuss how to access the content of a JavaScript object.
How to Access an Object’s Value
You cannot access the content of an object unless you put it in a variable and invoke it through the variable’s name.
Once you’ve kept your object in a variable, JavaScript provides two ways to invoke it:
- The dot notation
- The bracket notation
Let’s discuss the two techniques below.
How to use the dot notation to access an object’s value
One way to access an object’s value is to place a dot between the object’s variable name and its property’s key.
Here’s the syntax:
In the snippet above, variableName
references the name of the variable containing your object. On the other hand, propertyKey
refers to the name of the property whose value you wish to get.
Example: Use the dot syntax to access a properties object’s value
The profile.gender
code in the snippet above instructs the computer to get the value of the profile
object’s gender
property.
Let’s discuss the second way to access a JavaScript object’s value.
How to use the square bracket notation to access an object’s value
An alternate way to access an object’s value is to use a square bracket like this:
In the snippet above, variableName
references the name of the variable containing your object. On the other hand, propertyKey
refers to the name of the property whose value you wish to get.
Example: Use the square bracket syntax to access a properties object’s value
The profile["gender"]
code in the snippet above instructs the computer to get the value of the profile
object’s gender
property.
But what is the difference between the dot syntax and the square bracket notation? Let’s find out.
JavaScript object’s dot vs. square brackets notation – What’s the difference?
Here are the differences between the dot and square bracket syntax.
Difference 1: The dot syntax is easier to read
The dot syntax is cleaner and easier to read than the bracket notation. Therefore, most people prefer to use the dot syntax. However, the square bracket is more powerful.
Difference 2: Only the square notation can access properties with a number or multiword name
Suppose you used a number or multiword—e.g., "best colors"
—as your property’s name. In that case, you can use only the square bracket notation to access the property’s value.
Design and develop at the same time
Example: The dot syntax cannot access properties with a number name
The bookmark.100
’s invocation in the snippet above returned an error because you cannot use the dot syntax to access a property with a number or multiword name.
Now, consider another example.
Example: Use the square bracket syntax to access a property with a multiword name
The bookmark["Last Month"]
’s invocation correctly returned "December"
because we used a square bracket to access the multiword named property.
Let’s discuss the third difference between the dot and square bracket notations.
Difference 3: Only the square syntax can access properties through a variable’s value
You can use only the square bracket notation to access a property through a variable’s value.
For instance, consider this snippet:
By wrapping "gender"
in quotes, the computer will interpret the "gender"
string as a property’s name. Therefore, JavaScript will search the profile
object for a property named gender
.
Let’s now consider this other snippet:
By writing gender
without quoting it, the computer will interpret the word as a variable—not a property’s name. Therefore, JavaScript will search—locally or globally—for a gender
variable and invoke it.
Afterward, JavaScript will use the variable’s returned value as the property’s name to look for in the profile
object.
Example: Use a bracketed string to access an object’s value
The snippet above returned "Beauty"
because we used manager["wife"]
to tell the computer to get the manager
object’s wife
property’s value.
Let’s now consider another example.
Example: Use a bracketed variable to access an object’s value
The snippet above returned "Paul"
because JavaScript interpreted the unquoted wife
word as a variable. Therefore, the computer used the wife
variable’s value as the property to look for in the manager
object.
In other words, we used manager[wife]
to tell JavaScript to get the manager
object’s son
property’s value.
So, now that we know how to access a properties object’s value, we can discuss how to compute a property’s name.
Build your website with Namecheap
Computed Property Names in JavaScript
JavaScript allows you to compute (evaluate) a property’s name.
In other words, suppose you put an expression in a square bracket. In that case, the computer will calculate and use the result as the property’s name.
Example: Compute an object’s property’s name
You can see that JavaScript derived each of the doorNo
’s property names by concatenating the enSuites
and num
variables.
Let’s now discuss adding new properties to an object.
How to Add a New Property to a JavaScript Object
JavaScript provides two ways to add new properties to an object:
- The dot notation
- The bracket notation
Let’s discuss the two techniques below.
How to use the dot notation to add a new property to an object
One way to add new properties to a JavaScript object is to use the dot syntax as follows:
In the snippet above, existingObject
references the name of the variable containing your object.
newPropertyKey
refers to the name of the new property you wish to add to the existingObject
.
newPropertyValue
refers to the value you wish to assign to the newPropertyKey
.
Example: Use the dot syntax to add a new property to an object
The snippet above added a status
property to the profile
object with a "married"
string value.
Let’s now discuss the second way to add a new property to an object.
How to use the square bracket notation to add a new property to an object
An alternate way to add a new property to a JavaScript object is to use a square bracket like this:
In the snippet above, existingObject
references the name of the variable containing your object.
newPropertyKey
refers to the name of the new property you wish to add to the existingObject
.
newPropertyValue
refers to the value you wish to assign to the newPropertyKey
.
Example: Use the square bracket syntax to add a new property to an object
Note that JavaScript also allows using the square bracket notation to reference a variable.
Here’s an example:
You can see that the computer read the unquoted status
word as a variable—not a property’s name.
Let’s now see how to delete an object’s property.
Express Your Love for Coding!
How to Delete a Property from a Properties Object
You can use JavaScript’s delete
operator to delete a property from a properties object.
Here’s an example:
The square bracket alternative of the snippet above is like so:
Important Stuff to Know about JavaScript’s delete
Operator
Keep these five essential pieces of info in mind whenever you choose to use the delete
operator.
-
The
delete
operator always returnstrue
—except you use it for an own non-configurable property. -
Suppose the property you intend to delete does not exist. In such a case, the
delete
operator will not delete anything but will still returntrue
. -
The
delete
operator can only delete own properties. It has no effect on properties in an object’s prototype chain. Therefore, after the deletion of an own property, the prototype chain’s property—with the same name as the deleted own property—will still be callable. -
Avoid using the
delete
operator onArray
objects becausedelete
does not change an array’s length. The splice() method is a better way to remove array items. -
delete
is one of JavaScript’s unary operators.