An object literal is a list of zero or more pairs of property names and associated values of an object, enclosed in curly braces (
{}
).
The following is an example of an object literal. The first element of the
car
object defines a property,
myCar
, and assigns to it a new string, "
Toyota
"; the second element, the
getCar
property, is immediately assigned the result of invoking the function
carTypes('Honda')
; the third element, the
special
property, uses an existing variable (
sales
).
const
sales
=
"BMW"
;
function
carTypes
(
name
)
{
return
name
===
"Honda"
?
name
:
`
Sorry, we don't sell
${
name
}
.
`
;
}
const
car
=
{
myCar
:
"Toyota"
,
getCar
:
carTypes
(
"Honda"
)
,
special
:
sales
,
}
;
console
.
log
(
car
.
myCar
)
;
console
.
log
(
car
.
getCar
)
;
console
.
log
(
car
.
special
)
;