JavaScript Objects
Object oriented Programming in an important aspect of JavaScript. It is possible to use built-in objects available in JavaScript. It is also possible for a JavaScript programmer to define his own objects and variable types. In this JavaScript tutorial, you will learn how to make use of built-in objects available in JavaScript.
Built-in objects in JavaScript:
Some of the built-in objects available in JavaScript are:
- Date
- Math
- String
- Array
- Object
Of the above objects, the most widely used one is the String object.
Objects are nothing but special kind of data. Each object has Properties and Methods associated with it.
Properties of an Object:
Property is the value that is tagged to the object. For example let us consider one of the properties associated with the most popularly used String object – the length property. Length property of the string object returns the length of the string, that is in other words the number of characters present in the string.
General syntax of using the length property of the string object is as below:
variablename.length
Here variablename is the name of the variable to which the string is assigned and length is the keyword.
For example consider the JavaScript below:
|
The output of the above is
7
We see that the property of an object is the value associated with the object.
Method of an Object:
Method of an object refers to the actions than can be performed on the object. For example in String Object there are several methods available in JavaScript.
Some of the Methods associated with String Object are:
toUpperCase()
toLowerCase()
substring()
indexOf
lastIndexOf
All these methods are used to perform actions on the String object.
For example the substring method of the String Object in JavaScript is used for extracting a specific portion of the string.
If a programmer wants the value of String object in lowercase then the method toLowerCase() is used on String Object
To convert a string to upper case, the method toUpperCase() is used.
If a programmer wants to know the position of a characters or group of characters in a String Object it can be determined by applying indexof method to the String.
Example to understand how method can be used in an Object.
In the example below, we have used toUpperCase method of String object.
|
The output of the above script is
WELCOME
In the above script since the method toUpperCase is applied to the string object exf which has value initialized as Welcome all letters get converted as upper case and hence the output is as above.