Contact Us or call 1-877-932-8228


Objects contain one or more key-value pairs. The key portion can be any string. The value portion can be any type of value: a number, a string, an array, a function, or even another object.

Definition: When one of these values is a function, it's called a method of the object. Otherwise, they are usually called properties.

As it turns out, nearly everything in JavaScript is an object: arrays, functions, numbers, even strings, and they all have properties and methods.

Creating an Object Literal

An object literal is an object written in a shorthand syntax, using a pair of curly braces ( { } ) to surround a set of properties: value pairs, separated by commas. The values can be literal strings, numbers, booleans, functions, or nested literal objects, or values from variables or other expressions.

Code Sample:

var myObject = {
	sayHello : function() {

	myName : 'Rebecca'

myObject.sayHello();				// alerts 'hello'
alert(myObject.myName);   	// alerts 'Rebecca'
	<h1>Object Literals</h1>

When creating object literals, you should note that the key portion of each key-value pair can be written as any valid JavaScript identifier, a string (wrapped in quotes, which does not need to be a valid JavaScript identifier), or a number:

var myObject = {
	validIdentifier: 123,
	'some string': 456, // need quotes because of the space
	'class': 'abc',     // because class is a reserved word
	99999: 789

Object literals can be extremely useful for code organization.

Accessing Elements of an Object

You can work with properties and methods of an object by either:

  • Using a dot after the object variable name, followed by the property or method name.
  • Using associative array notation, using the property or method name as a string index.
    This form is particularly useful when the element name comes from a variable.