Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

Why call it "object literal" instead of "object variable"?

we are accustomed to use the word variable, does calling it a "literal" make it any different?

2 Answers

Brent Suggs
seal-mask
PLUS
.a{fill-rule:evenodd;}techdegree seal-36
Brent Suggs
Front End Web Development Techdegree Graduate 21,343 Points

I believe "object literal" refers to the actual coma separated key:value pairs in the object, not the object itself. Just like an variable you have the variable name that you refer to as the variable, but the value would be the variable literal... the literal value of the variable. So an object literal would be the literal definition or key:value pairs of the object. But that is just my understanding thus far...

Sean T. Unwin
Sean T. Unwin
28,660 Points
Literals: "You use literals to represent values in JavaScript. These are fixed values, not variables, that you literally provide in your script."
Object Literal: "An object literal is a list of zero or more pairs of property names and associated values of an object, enclosed in curly braces ({}). "

-- from MDN's definition of JavaScript Literals


I could see how this could be confusing because a variable has 2 parts so when we make, or instantiate, a variable we are doing multiple tasks. A variable represents something, including null or undefined. This something can be a type of specific container. These containers are Strings, Numbers, Booleans, Arrays, Regular Expressions and Objects. The containers are created by using specific syntax or characters. A couple of these methods are using square brackets to create an Array or curly brackets to create an Object. The containers are filled by assigning values. For example, A String is created with a set of quotation marks and assigned a value of some characters between them - "This is a String."

We may define a variable by giving it a name, or identifier, (var myArray) then reference something to it ( place an equals sign (=) then create a container ( an Array in this case - [] ) then between the brackets we insert our values ( 'Literals', 'Variables', 'Objects'). Which altogether looks like, var myArray = ['Literals', 'Variables', 'Objects'];. The phrase, ['Literals', 'Variables', 'Objects'], is an Array Literal.

The 2 parts of a Variable are Identifier and Literal which are separated by an equal sign: var [Identifier] = [Literal];

An "object literal" and an "object variable" are different concepts. An "object literal" is the object itself and it's contents, whereas an "object variable", in my opinion, would be the variable that is assigned to the object literal. Therefore, an "object variable" contains an "object literal" as well as an identifier for the literal.