JS Tidbits: Literals

2 comments | Posted: 30 October 06 in Tutorials, by Cody Lindley

The JavaScript object literal is certainly in style these days. In fact it’s hard to find a JavaScript library that does not use the object literal. So the following code should be of no surprise.

var myBodyObject = {legs:2, arms:2, toes:10, fingers:10};

Now, what might surprise the non-scripting expert is that JavaScript has several literals. For example, all of the following JS expressions are formatted in the object literal. Meaning they are data values which appear directly in a program and are literally the values themselves.

var myNumber = 432;
var myString = "This is a string";
var myBoolean = true;
var myFunction = function (){return this.location};
var myRegExp = /javascript/gi;
var myArray = [1,2,3,4,5];
var myBodyObject = {legs:2, arms:2, toes:10, fingers:10};

All of the literals above, if you don’t already know, create an object. This is not to be confused with an object object, such as the last expression in the previous example. In JavaScript, when you define a variable (ex: var myNumber) and then add a value to the variable identifier with the = operator, JavaScript will create a number object. This is why people often say that everything in JavaScript is an object. This idea that everything in JavaScript is an object is not exactly clear until you grasp the difference between a literal expression, and non-literal expression. The literal expressions I have already shown you can also be written like so:

var myNumber = new Number(432);
var myString = new String("This is a string");
var myBoolean = new Boolean(true);
var myFunction = new Function("", "return this.location")
var myRegExp = new RegExp("javascript");

var myArray = new Array();
myArray[0] = 1;
myArray[1] = 2;
myArray[2] = 3;
myArray[3] = 4;
myArray[4] = 5;
var myBodyObject = new Object();
myBodyObject.legs = 2;
myBodyObject.arms = 2;
myBodyObject.toes = 10;
myBodyObject.fingers = 10;

Notice the use of the new keyword. Each expression in its non-literal format demonstrates why everything in JavaScript is an object. Not to mention the fact that such datatypes like string and number have properties and methods. This means a string and a number have a constructor or class that they instantiate from. Thus, a number is really an object, as well a number belonging to the number datatype. I will elaborate on this in another Tidbit. The point of this Tidbit was to simply show literal expressions written in a non-literal format. This is important to understand if you are going to understand the purpose of an object object, which I will cover in a future article.

As a side note, you should be aware that in JavaScript the date object and error object have no literal equivalent.

Discuss This Topic

  1. 1 Nate K

    Short but sweet. This is very useful for those trying to wrap their heads around JS. Some may realize it indirectly, as you can perform certain methods on objects (such as numbers and string, etc) throughout your scripts (and is done often in tutorials).

    Thanks for the small tidbit.

  2. 2 Lance Fisher

    Cool. Very clear, concise, and helpful.


Comments closed after 2 weeks.