JavaScript Objects Introduction
JavaScript Objects Introduction
An OOP language allows you to define your own objects and make your own variable types.
JavaScript is an Object Oriented Programming (OOP) language. An OOP language allows you to define your
own objects and make your own variable types.
However, creating your own objects will be explained later, in the Advanced JavaScript section. We will start
by looking at the built-in JavaScript objects, and how they are used. The next pages will explain each built-
in JavaScript object in detail.
Note that an object is just a special kind of data. An object has properties and methods.
Properties
In the following example we are using the length property of the String object to return the number of
characters in a string:
<script type="text/javascript">
var txt="Hello World!";
document.write(txt.length);
</script>
12
Methods
In the following example we are using the toUpperCase() method of the String object to display a text in
uppercase letters:
<script type="text/javascript">
var str="Hello world!";
document.write(str.toUpperCase());
</script>
HELLO WORLD!
JavaScript String Object
Examples
Style strings
How to style strings.
For a complete reference of all the properties and methods that can be used with the String object, go to
our complete String object reference.
The reference contains a brief description and examples of use for each property and method!
String object
Examples of use:
The following example uses the length property of the String object to find the length of a string:
12
The following example uses the toUpperCase() method of the String object to convert a string to uppercase
letters:
HELLO WORLD!
Examples
getTime()
Use getTime() to calculate the years since 1970.
setFullYear()
How to use setFullYear() to set a specific date.
toUTCString()
How to use toUTCString() to convert today's date (according to UTC) to a string.
getDay()
Use getDay() and an array to write a weekday, and not just a number.
Display a clock
How to display a clock on your web page.
For a complete reference of all the properties and methods that can be used with the Date object, go to our
complete Date object reference.
The reference contains a brief description and examples of use for each property and method!
Note: The Date object will automatically hold the current date and time as its initial value!
Set Dates
We can easily manipulate the date by using the methods available for the Date object.
In the example below we set a Date object to a specific date (14th January 2010):
And in the following example we set a Date object to be 5 days into the future:
Note: If adding five days to a date shifts the month or year, the changes are handled automatically by the
Date object itself!
The following example compares today's date with the 14th January 2010:
Examples
Create an array
Create an array, assign values to it, and write the values to the output.
For...In Statement
How to use a for...in statement to loop through the elements of an array.
For a complete reference of all the properties and methods that can be used with the Array object, go to our
complete Array object reference.
The reference contains a brief description and examples of use for each property and method!
Create an Array
There are two ways of adding values to an array (you can add as many values as you need to define as
many variables you require).
1:
You could also pass an integer argument to control the array's size:
2:
Note: If you specify numbers or true/false values inside the array then the type of variables will be numeric
or Boolean instead of string.
Access an Array
You can refer to a particular element in an array by referring to the name of the array and the index
number. The index number starts at 0.
document.write(myCars[0]);
To modify a value in an existing array, just add a new value to the array with a specified index number:
myCars[0]="Opel";
document.write(myCars[0]);
Opel
The Boolean object is used to convert a non-Boolean value to a Boolean value (true or false).
Examples
For a complete reference of all the properties and methods that can be used with the Boolean object, go to
our complete Boolean object reference.
The reference contains a brief description and examples of use for each property and method!
Note: If the Boolean object has no initial value or if it is 0, -0, null, "", false, undefined, or NaN, the object
is set to false. Otherwise it is true (even with the string "false")!
All the following lines of code create Boolean objects with an initial value of false:
And all the following lines of code create Boolean objects with an initial value of true:
Examples
round()
How to use round().
random()
How to use random() to return a random number between 0 and 1.
max()
How to use max() to return the number with the highest value of two specified numbers.
min()
How to use min() to return the number with the lowest value of two specified numbers.
For a complete reference of all the properties and methods that can be used with the Math object, go to our
complete Math object reference.
The reference contains a brief description and examples of use for each property and method!
Math Object
var pi_value=Math.PI;
var sqrt_value=Math.sqrt(16);
Note: Math is not a constructor. All properties and methods of Math can be called by using Math as an
object without creating it.
Mathematical Constants
JavaScript provides eight mathematical constants that can be accessed from the Math object. These are: E,
PI, square root of 2, square root of 1/2, natural log of 2, natural log of 10, base-2 log of E, and base-10 log
of E.
You may reference these constants from your JavaScript like this:
Math.E
Math.PI
Math.SQRT2
Math.SQRT1_2
Math.LN2
Math.LN10
Math.LOG2E
Math.LOG10E
Mathematical Methods
In addition to the mathematical constants that can be accessed from the Math object there are also several
methods available.
The following example uses the round() method of the Math object to round a number to the nearest
integer:
document.write(Math.round(4.7));
The following example uses the random() method of the Math object to return a random number between 0
and 1:
document.write(Math.random());
0.8783551517518385
The following example uses the floor() and random() methods of the Math object to return a random
number between 0 and 10:
document.write(Math.floor(Math.random()*11));
What is RegExp
When you search in a text, you can use a pattern to describe what you are searching for. RegExp IS this
pattern.
A more complicated pattern consists of more characters, and can be used for parsing, format checking,
substitution and more.
You can specify where in the string to search, what type of characters to search for, and more.
Defining RegExp
We define a RegExp object with the new keyword. The following code line defines a RegExp object called
patt1 with the pattern "e":
When you use this RegExp object to search in a string, you will find the letter "e".
test()
The test() method searches a string for a specified value. Returns true or false
Example:
var patt1=new RegExp("e");
document.write(patt1.test("The best things in life are free"));
Since there is an "e" in the string, the output of the code above will be:
true
Try it yourself
exec()
The exec() method searches a string for a specified value. Returns the text of the found value. If no match
is found, it returns null
Example 1:
var patt1=new RegExp("e");
document.write(patt1.exec("The best things in life are free"));
Since there is an "e" in the string, the output of the code above will be:
Try it yourself
Example 2:
You can add a second parameter to the RegExp object, to specify your search. For example; if you want to
find all occurrences of a character, you can use the "g" parameter ("global").
For a complete list of how to modify your search, visit our complete RegExp object reference.
When using the "g" parameter, the exec() method works like this:
Since there is six "e" letters in the string, the output of the code above will be:
eeeeeenull
Try it yourself
compile()
compile() can change both the search pattern, and add or remove the second parameter.
Example:
var patt1=new RegExp("e");
document.write(patt1.test("The best things in life are free"));
patt1.compile("d");
document.write(patt1.test("The best things in life are free"));
Since there is an "e" in the string, but not a "d", the output of the code above will be:
truefalse
Try it yourself
For a complete reference of all the properties and methods that can be used with the RegExp object, go to
our complete RegExp object reference.
The reference contains a brief description and examples of use for each property and method including the
string object
In addition to the built-in JavaScript objects, you can also access and manipulate all of the HTML
DOM objects with JavaScript.
Follow the links to learn more about the objects and their collections, properties, methods and events.
Object Description
Window The top level object in the JavaScript hierarchy. The Window object represents a
browser window. A Window object is created automatically with every instance of
a <body> or <frameset> tag
Navigator Contains information about the client's browser
Screen Contains information about the client's display screen
History Contains the visited URLs in the browser window
Location Contains information about the current URL
The HTML DOM is a W3C standard and it is an abbreviation for the Document Object Model for HTML.
The HTML DOM defines a standard set of objects for HTML, and a standard way to access and manipulate
HTML documents.
All HTML elements, along with their containing text and attributes, can be accessed through the DOM. The
contents can be modified or deleted, and new elements can be created.
The HTML DOM is platform and language independent. It can be used by any programming language like
Java, JavaScript, and VBScript.
Follow the links below to learn more about how to access and manipulate each DOM object with JavaScript:
Object Description
Document Represents the entire HTML document and can be used to access all elements in
a page
Anchor Represents an <a> element
Area Represents an <area> element inside an image-map
Base Represents a <base> element
Body Represents the <body> element
Button Represents a <button> element
Event Represents the state of an event
Form Represents a <form> element
Frame Represents a <frame> element
Frameset Represents a <frameset> element
Iframe Represents an <iframe> element
Image Represents an <img> element
Input button Represents a button in an HTML form
Input checkbox Represents a checkbox in an HTML form
Input file Represents a fileupload in an HTML form
Input hidden Represents a hidden field in an HTML form
Input password Represents a password field in an HTML form
Input radio Represents a radio button in an HTML form
Input reset Represents a reset button in an HTML form
Input submit Represents a submit button in an HTML form
Input text Represents a text-input field in an HTML form
Link Represents a <link> element
Meta Represents a <meta> element
Option Represents an <option> element
Select Represents a selection list in an HTML form
Style Represents an individual style statement
Table Represents a <table> element
TableData Represents a <td> element
TableRow Represents a <tr> element
Textarea Represents a <textarea> element