Take the 2-minute tour ×

Well, I'm completely new to JavaScript. Can you please tell me what type of data is this JavaScript code:

var options =
    sourceLanguage: 'en',
    destinationLanguage: ['hi', 'bn', 'fa', 'gu', 'kn', 'ml', 'mr', 'ne', 'pa', 'ta','te','ur'],
    shortcutKey: 'ctrl+g',
    transliterationEnabled: true

I've reviewed JavaScript arrays, but it doesn't seem to be a traditional array. Still don't know if it's some kind of arrays or another data type!!

Additionally, is there any way to set individual elements to that data type such as setting array elements individually.

Thanks in advance

share|improve this question
It's an object, which is pretty much the most common data type in javascript. –  hobbs Apr 2 '10 at 8:31
add comment

5 Answers

up vote 7 down vote accepted

This is a way of creating and initialising an object in javascript called an object literal. Since javascript is dynamically typed, you can add key/values at any time, even to the built in objects.

The equivalent code for this would be:

var options = {};
options.sourceLanguage = 'en';
options.destinationLanguage = ['hi', 'bn', 'etc'];

The square brackets [] denote an array. The equivalent for this would be

var destinationLanguage = [];
destinationLanguage.push('bn'); //etc

You access array elements by index eg destinationLanguage[0].

As you can see it is much more readable and convenient to to initialise everything using the notation in your request.

This notation forms the basis of something called JSON (Javascript Object Notation) which is wire format for passing information eg between client and server. The string in your example could be retrieved via an AJAX request and parsed in a number of ways into a complex object.

share|improve this answer
add comment

It's a Javascript object. In a JS console you can check its type:

>>> typeof(options)

JS objects are sometimes used as simple associative arrays (like hash tables or dictionaries in other languages). The code snippet you present here is probably for such a use. Read more on the technique in this tutorial.

Also, this tutorial is very good.

share|improve this answer
add comment

it is an object literal in other word,its a shorcut to:

var options = new Object();
options.sourceLanguage = 'en';
options.shortcutKey = 'ctrl+g'; 
options.transliterationEnabled = true
share|improve this answer
+1 For using the correct terminology –  Steve Apr 2 '10 at 18:47
/agree ........ –  James Westgate Apr 3 '10 at 11:21
add comment

In JavaScript you can create singleton objects using JavaScript Object Notation (JSON):

var o = {
    prop1 : 'value',
    func1 : function() {
        this.prop1 = 'some other value';

Then you could do the following:

alert(o.prop1); // displays 'value' in an alert box in a browser environment
alert(o.prop1); // displays 'some other value' in an alert box

So yes, it's an object. Of course so is everything else in JavaScript. And yes, it can be used as a hash or dictionary.

share|improve this answer
That's not JSON, that's Object Literal Notation. –  insin Apr 2 '10 at 9:00
Quite right. Of course JSON is based on JavaScript Object Literal Notation. Hence the name JSON. –  Benry May 1 '10 at 0:06
add comment

Also called a dictionary in many languages. You have a key-value pair so you would be able to access your keys like:

value = dictionary[key]

or in your case:

lang = options['sourceLanguage'];

Writing is just the same but backwards:

options['sourceLanguage'] = lang;

The only difference with your example is that it's doing it all in batch. All at once.

share|improve this answer
add comment

Your Answer


