JavaScript Reference: Objects


Creating Objects

Objects are containers than can be given variable names, and can contain many properties surrounded by curly brackets {}. Each of these properties appears in the format name: value where name is the name of the property (and is completely up to you) and value is a string or number.
var bulldogObject = { breed: "English Bulldog", weight: 45, color: "brindle", name: "Rex" }

The properties of an object can be accessed in two ways:
bulldogObject.weight			// returns 45
bulldogObject["weight"]		// returns 45
bulldogObject.color			// returns "brindle"
bulldogObject["color"]		// returns "brindle"

We can also create a method for our object, which is stored in its properties as a function definition.
// Add a method to the object called 'printInfo'
var bulldogObject = { breed: "English Bulldog", weight: 45, color: "brindle", name: "Rex", printInfo: function() {console.log("I am a "+this.breed+" name "+this.name)}}
bulldogObject.printInfo()
would result in
I am a English Bulldog name Rex

Global Objects

Global objects are 'predefined' in JavaScript. These objects have their own methods and properties, many of which are listed in detail on these pages. For instance, a String object has properties like 'length' and methods like 'trim()', and so any string you create is interpreted as a String object and inherits those properties.

Global Values

ValueDescription
InfinityPositive infinity
NaNNot a number
undefinedUndefined variable or function has been called
nullEmpty value without type

Global Functions

FunctionUseExampleExample Returns
eval()Evaluate a JS expression and return as a string
NOTE: AVOID THIS FUNCTION
eval(6+5)'11'
isFinite()Returns true if value is finite, false otherwiseisFinite(3.14)true
isNaN()Returns true if value is not a number, false otherwiseisNaN('hello')false
parseFloat()Parses string as a floating point numberparseFloat('3.14')
parseFloat('hello')
3.14
NaN
parseInt()Parses string as integer with optional baseparseInt(25,3)
parseInt("Hello", 8)
2
NaN

Strings, Dates, and Numbers

ObjectDescription
StringWrapper for working with text (see Strings section for more)
RegExpCreates regular expression for string pattern matching (see Strings section for more)
NumberWrapper for working with numbers (see Math and Numbers section for more)
MathBuilt in object for mathematics (see Math and Numbers section for more)
DateObjects based on moment in time, measured in number of milliseconds since 1 January, 1970 UTC.