Did you find this useful?
Socialize it today.

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();

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: myObj.name="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.

Please help us spread the word by socializing it today!

email contact us

Did you find something wrong with the information on this page? Please take a moment to report it to us so that we can continue to improve the quality of the information on this site. Click here to report an issue with this page.

Recommended Books & Training Resources

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