JavaScript Objects, Methods and Properties

Thursday, April 19, 2012

While JavaScript is not a complete, full-blown object oriented programming (OOP) language like Java, it is an object based language. When you write object-oriented code, you have more control and flexibility. Rather than writing procedural code which is used just one time, object based languages allow you to reuse code over and over again.


An object is a collection of properties. These properties can be primitive data types, other objects, or methods (functions). A constructor function is a type of function used to create an object. Fortunately, JavaScript comes with many built-in objects that we can use. Some examples include the Image, String, Math, Date, and Array. Here is an example of creating a new custom object:

var myObj = new Object();"John";

Once we create our new myObj object, we can access its properties such as "name" and "age". For example, if we want to assign the name of the myObj, we would simply assign it as follows:"John".


In the previous example, you just saw that we assigned the name of the new myObj object a value. The "name" is a property of the myObj object. Properties are the values associated with an object. In this next example we will use the length property of the String object to return the number of characters in the string.

var txt="JavaScript is great!";

If you were to execute this JavaScript code, the result would be that the number 20 will be displayed on the screen.


Methods are the actions that can be performed on objects. Methods are functions of the object. In the following example we are using the bold() method of the String object to display the string text in bold styling:

var txt="JavaScript is great!";

If you were to execute this JavaScript code, the result would be that the string will be displayed with bold styling: JavaScript is great!.

Always keep in mind that JavaScript is case sensitive.

Did you find the page informational and useful? Share it using one of your favorite social sites.

Recommended Books & Training Resources

HTML CSS and JavaScript Editor Professional JavaScript for Web Developers JavaScript and jQuery: The Missing Manual