46

In Javascript: The Good Parts Douglas Crockford writes that one of Javascript's good ideas is "expressive object literal notation." I understand that he is basically complimenting JSON.

But what is "literal" about this notation? Are there are other languages that use "expressive object literal notation?" Are there languages that don't? What exactly does he mean?

bernie2436
  • 22,841
  • 49
  • 151
  • 244

5 Answers5

36

About "complimenting JSON": He specified it.

The "literal" part: Googling "object literal" provides two top resources: MDN and Wikipedia. To quote the latter:

In computer science, a literal is a notation for representing a fixed value in source code. Almost all programming languages have notations for atomic values such as integers, floating-point numbers, and strings, and usually for booleans and characters; some also have notations for elements of enumerated types and compound values such as arrays, records, and objects.

Basically, all syntax constructs whose use lead to a defined type can be called a literal. (E.g., a string literal, "abc".) It's a technical term that denotes, that "literally" writing something in this or that way leads to a certainly typed variable exclusively (in contrast to constructs, that look like something else, like array() in PHP).

Boldewyn
  • 81,211
  • 44
  • 156
  • 212
  • so for instance, in a statically typed language, a string variable is a variable that can hold a string. A string literal fills the string variable. In javascript, objects can be expressed as literals -- like string literals in my example. Is this the idea? – bernie2436 Jan 08 '14 at 14:40
  • I like this answer. Very clean and focusing on the question. Have my upvote. – vinczemarton Jan 08 '14 at 14:49
  • @akh2103 basically yes. Sounds right. You don't need to instantiate any class or so to get something of type `foo`, but can just type away, just like strings and booleans. In Python, to give another example, you could use `my_dict = dict()` (not literal) or `my_dict = {}` (literal notion) to get a variable `my_dict` of type `dict`. – Boldewyn Jan 08 '14 at 15:10
10

Well, in programming in general a literal is a fixed value. Like saying var five = 5; and using "five" in some math, just use the number 5 literally.

So in an OOP language an object literal would be something like:

var new_dog = {
    name: "doggy",
    good_dog: false
};

The entire thing is my object. Things between my {} are my literals. My notation is a pattern "name:value".

evgeni fotia
  • 4,650
  • 3
  • 16
  • 34
Aage Torleif
  • 1,907
  • 1
  • 20
  • 37
10

As someone who is new to programming I've really struggled with the idea of literals. Explanations often assume that you already know all about types and constructors. So this is my (over)simplified take on it:

By 'type', we mean the classification of the variable (different types of variables can do different things). ​It's common to talk about primitive-type literals​ such as strings, numbers, and booleans. Here is a string literal example:

let myStr = 'word'; 

In this case word is a string literal; you have created something that is literally of the string type.

In JS you can have object literals as well. That is you can just type out this thing between curly braces and it is automatically a thing of object-type.

let mylitteralObject = {};

We have created an object without using an operator (an operator is essentially a built in function using familiar syntax, like '+'). The JS engine looks at the curly braces and works out that you are declaring an object.

A non literal way is to use an object constructor - a function that is combined with the new keyword to make a new object. ​

let myConstructedObject =  new Object();

Here the new key word is treated as an operator. And as such, the data inside myConstructedObject is the result of an expression evaluation.

cham
  • 8,666
  • 9
  • 48
  • 69
6

An object literal is a comma separated list of name value pairs wrapped in curly braces. In JavaScript an object literal is defined as follows:

var someObject = {
    some_prop: 'some string value',
    another_prop: 'another string value',
    int_prop: 100
};

It is “flat”. You create it, you add properties and methods to it, and all of those properties and methods are public . 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 could be no comma after the last name/value pair, But in this case Internet Explorer prior to version 9 will generally trigger an error: 'Expected identifier, string or number'.

Values can be of any data type, including array literals, functions, and nested object literals .

Although a JavaScript object literal and a JavaScript instance object are both objects, they differ in their inherent nature and features

Alexander
  • 12,424
  • 5
  • 59
  • 76
  • The third bullet point of your answer is inaccurate. Commas after the last pair are allowed in JavaScript (see [section 11.1.5 of the specification](http://es5.github.io/#x11.1.5)), but not in ECMAScript 3. IE 8 is an ancient browser, released in March 2009 while ES 5.0 was published in December 2009. So, when IE8 was released, it did conform with the (then) latest specification (ES3), and therefore it did not trailing commas. IE8 is the latest supported IE version on XP (which is still popular, despite being close to EOL), so the recommendation to omit the comma is valid though. – Rob W Jan 08 '14 at 17:44
5

I've been struggling with this myself. While I found the answers above helpful, I found a simple definition for literal in Webopedia:

In programming, a value written exactly as it's meant to be interpreted. In contrast, a variable is a name that can represent different values during the execution of the program. And a constant is a name that represents the same value throughout a program. But a literal is not a name -- it is the value itself.

A literal can be a number, a character, or a string. For example, in the expression,

x = 3

x is a variable, and 3 is a literal.

What really stuck out to me was

But a literal is not a name -- it is the value itself.

Additionally, this StackOverflow post is also very helpful. I appreciated this comment:

Basically code that represents a value in a program instead of having special meaning for the program to execute. For example: 15 literally means fifteen in the program vs + which has the special meaning of "add two things together". You wouldn't treat + as representing the plus sign itself since it is not a literal.

And this answer was helpful:

Quick example:

int my_int_var = 723;

723 - This set of characters refers to a literal integer value.

my_int_var - This set of characters refers to a variable integer value.

Community
  • 1
  • 1
Dani Amsalem
  • 1,266
  • 17
  • 26