Discussions

Could you explain an object literal as simply as possible please?
Could you explain an object literal as simply as possible please? First, let's understand what are "literals" in programming. literal : a value written exactly as it's meant to be interpreted. In other words, a literal is a data value that is used directly in code. Examples in JS: Integer literals — 2, -2 Floating-Point literals — 2.5, -2.2 String literals — "", "Hello, World!" Boolean literals — true, false null literal — null undefined literal — undefined Array literals — [], [2, 2.5, -2] Object literals — {}, {n1: 2, n2: 2.5, n3: -2} RegExp literals — /ab+c/ Edit: After being enlightened by u/senocular (see his comment below), I've deleted undefined literal. I have added Floating-Point and RegExp literals based on the mdn web docs article on Literals . Extremely sorry for the incorrect information about undefined; I should have done better. More on reddit.com
🌐 r/learnjavascript
5
2
November 28, 2022
javascript - How to create a method in object literal notation? - Stack Overflow
Can you please tell me how to do ... object literal notation. ... Please forget about w3schools. It's a terrible resource. Use MDN instead. And instead of new Object(), you would better simply use {}. ... If you want to use functions on your object instances (like creating several person objects) then defining the same function for every object isn't realy effective. In JavaScript you can use ... More on stackoverflow.com
🌐 stackoverflow.com
What do people mean when they say "object literal" in JavaScript? - Stack Overflow
I know that most everything is an object in JavaScript. When people say "object literal," do they mean an object like this? var thing = { 'foo': 'bar' , 'baz': 'foo' , 'bar': 'baz' }; More on stackoverflow.com
🌐 stackoverflow.com
javascript - What is the meaning of "literal" in the phrase object literal notation? - Stack Overflow
The "literal" part: Googling "object literal" provides two top resources: MDN and Wikipedia. More on stackoverflow.com
🌐 stackoverflow.com
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Learn_web_development › Core › Scripting › Object_basics
JavaScript object basics - Learn web development | MDN
An object like this is referred to as an object literal — we've literally written out the object contents as we've come to create it.
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Grammar_and_types
Grammar and types - JavaScript - MDN Web Docs - Mozilla
You can call any of the String object's methods on a string literal value. JavaScript automatically converts the string literal to a temporary String object, calls the method, then discards the temporary String object.
🌐
Amberwilson
amberwilson.co.uk › learn › object literals
Object Literals | Amber Wilson
Object literals are a data structure that can be used in JavaScript. I really like seeing how data can be organised and object literals do this really nice and tidily. Here's an example: var cat = { myCat: 'Tabby', getCat: catTypes('Siamese'), ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Glossary › Object
Object - Glossary - MDN Web Docs
July 11, 2025 - In JavaScript, objects can be seen as a collection of properties. With the object literal syntax, a limited set of properties are initialized; then properties can be added and removed. Property values can be values of any type, including other objects, which enables building complex data structures.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Glossary › Literal
Literal - Glossary - MDN Web Docs
July 11, 2025 - ... A string literal is zero or ... or both double quotation marks). ... An object literal is a list of zero or more pairs of property names and associated values of an object, enclosed in curly braces ({})....
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Object
Object - JavaScript - MDN Web Docs
The Object type represents one of JavaScript's data types. It is used to store various keyed collections and more complex entities. Objects can be created using the Object() constructor or the object initializer / literal syntax.
Find elsewhere
🌐
Reddit
reddit.com › r/learnjavascript › could you explain an object literal as simply as possible please?
Could you explain an object literal as simply as possible please? : r/learnjavascript
November 28, 2022 - The stuff inside and including the {} is the object literal. The assignment makes it useful. A clear explanation of the JavaScript Event Loop (without oversimplifying it)
🌐
Unibo
lia.disi.unibo.it › materiale › JS › developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Object_initializer.html
Object initializer - JavaScript | MDN
Objects can be initialized using ... (initializer notation). An object initializer is a list of zero or more pairs of property names and associated values of an object, enclosed in curly braces ({})....
🌐
SitePoint
sitepoint.com › blog › es6 › es6 in action: enhanced object literals
ES6 in Action: Enhanced Object Literals — SitePoint
November 7, 2024 - ES6 Enhanced Object Literals introduce several key features that simplify the process of working with objects in JavaScript. These include shorthand property names, which allow you to define properties without repeating the property name, and shorthand method names, which simplify the syntax for defining methods.
🌐
W3Schools
w3schools.com › js › js_objects.asp
JavaScript Objects
In JavaScript, the this keyword refers to an object · It is used to access the object that is calling a method · Step 5Beginner · Displaying properties by name · Displaying properties in a loop · Using Object.values() Using JSON.stringify() Step 6Beginner · Sometimes we need to create many objects of the same type. To create an object type we use an object constructor function. Step 1Advanced · Using an Object Literal ·
🌐
Unibo
lia.disi.unibo.it › materiale › JS › developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Working_with_Objects.html
Working with objects - JavaScript | MDN
Object initializers are expressions, and each object initializer results in a new object being created whenever the statement in which it appears is executed. Identical object initializers create distinct objects that will not compare to each other as equal. Objects are created as if a call to new Object() were made; that is, objects made from object literal expressions are instances of Object.
🌐
Medium
medium.com › @rabailzaheer › enhancing-objects-advanced-object-literals-in-javascript-3a99bc651bac
Enhancing Objects: Advanced Object Literals in JavaScript
September 29, 2023 - An object literal is a simple way to create and define objects in JavaScript. It consists of a collection of key-value pairs enclosed within curly braces {}. Each key represents a property, and each value is the property's corresponding value.
Top answer
1 of 8
228

Computed property names are supported in ECMAScript2015:

var name = 'key';
var value = 'value';
var o = {
  [name]: value
};
console.log("o as json : " + JSON.stringify(o));

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer

2 of 8
112

Prior to ECMAScript 2015 (ed 6), an object literal (ECMAScript calls it an "object initializer") key must be one of:

  1. IdentifierName
  2. StringLiteral
  3. NumericLiteral

So you couldn't use an expression as the key in an initialiser. This was changed as of ECMAScript 2015 (see below). You could use an expression with square bracket notation to access a property, so to set the properties with an expression you had to do:

var required = { directories : {}};
required.directories[this.applicationPath] = "Application " + this.application + " does not exists";
required.directories[this.applicationPath + "/configs"] = "Application config folder does not exists";
...

and so on. Since this.applicationPath is reused a lot, better to store a reference to help with performance and cut down the amount of code:

var a = this.applicationPath;
var required = { directories : {}};
var rd = required.directories;
rd[a] = "Application " + this.application + " does not exists";
rd[a + "/configs"] = "Application config folder does not exists";
...

Edit

As of ECMAScript 2015 (ed 6), object initializers can have computed keys using:

[expression]: value

There is also shorthand syntax for property and method names.

See MDN: Object Initializer or ECMAScript Object Initializer.

🌐
Better Programming
betterprogramming.pub › object-literal-in-javascript-d3e0e7d58f3b
Object Literals in JavaScript. These come up in interviews more than… | by Peterson C | Better Programming
January 13, 2020 - Here’s a snippet of an object literal with one property and one function. var greeting = { fullname: "Michael Jackson", greet: (message, name) => { console.log(message + " " + name + "!!"); } }; Here’s the snippet to use the object we just created to log the fullname and call the greet function with the fullname value.
🌐
DEV Community
dev.to › og_dev › level-up-your-js-object-literal-enhancements-that-will-change-your-code-jbc
Level Up Your JS: Object Literal Enhancements That Will Change Your Code - DEV Community
October 8, 2024 - This feature is powerful when you need to create objects with dynamic keys based on some logic or external data. MDN Documentation: Computed property names · ES6 introduced a new way to define methods in object literals using the get and set keywords.