Objects

An object is just a special kind of data, with Properties and Methods.

JavaScript allows you to define your own objects.

          // To declare an object
var person={ firstname : "John", lastname  : "Smith", age:  25};

// Use spaces and line breaks to make your definition clearer
var person={
    firstname : "John",
    lastname  : "Smith",
    age       :  25
};

// You can access object properties in two way
var age = person.age;
var age = person["age"]; 

        

The object (person) in the example above has 3 properties: firstname, lastname, and age.

See also for in and forEach.

Properties

Properties are the values associated with an object.

The syntax for accessing the property of an object is:

          objectName.propertyName 

        

This example uses the length property of the String object to find the length of a string:

          var message = "Hello World!";
var x = message.length; 

        

The value of x, after execution of the code above will be 12.

Methods

Methods are the actions that can be performed on objects.

You can call a method with the following syntax:

          objectName.methodName() 

        

This example uses the toUpperCase() method of the String object, to convert a text to uppercase:

          var message="Hello world!";
var x = message.toUpperCase(); 

        

The value of x, after execution of the code above will be “HELLO WORLD!”.