Yes, that's exactly what object literal means. A definition of a javascript object which may contain comma separated definition of properties, functions, ...
Answer from Darin Dimitrov on Stack OverflowObject literals are formed using the following syntax rules:
- A colon separates property name from value.
- A comma separates each name/value pair from the next.
- There should be no comma after the last name/value pair. Firefox won't object if you add it, but Internet Explorer will trigger an error: 'Expected identifier, string or number'.
Yes, that's exactly what object literal means. A definition of a javascript object which may contain comma separated definition of properties, functions, ...
Object literals are formed using the following syntax rules:
- A colon separates property name from value.
- A comma separates each name/value pair from the next.
- There should be no comma after the last name/value pair. Firefox won't object if you add it, but Internet Explorer will trigger an error: 'Expected identifier, string or number'.
An object literal is a way to declare an object.
You would write
var myObject = {}; // with or without members
instead of
var myOject = new Object();
You can also use array literals:
var myArray = [];
instead of
var myArray = new Array(); // with or without members
It's shorter, of course but it also bypasses the constructor so it's supposed to be more efficient.
Having Difficulty Understanding Object Literals in JS
Could you explain an object literal as simply as possible please?
understanding object literals
What's the difference between an object literal and JSON? When do you use which?
Videos
hi guys,
trynna understand something that hasn't been made clear to me regarding the content of object literals. These always come in the form of name:value pairs. But i bumped into an object example:
const animal = {
type: "cat",
name: "kitty",
sounds() { console.log("meow meow") } };why is the function not listed as a name:value pair? Should it not be something along the lines of meow: sounds() { console.log("meow meow") }? Why is it exempt. Is there any way we can list it like that? And if yes, what would be the difference?