JavaScript is an Object Oriented Programming (OOP) language.
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 are the values associated with an object.
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!" </script> |
The output of the code above will be:
12 |
Methods are the actions that can be performed on objects.
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!" </script> |
The output of the code above will be:
HELLO WORLD! |
The String object is used to manipulate a stored piece of text.
Return the length of a string
How to use the length property to find the length of a string.
Style strings
How to style strings.
The indexOf() method
How to use the indexOf() method to return the position of the first occurrence of a specified string value in a string.
The match() method
How to use the match() method to search for a specified string value within a string and return the string value if found
Replace characters in a string - replace()
How to use the replace() method to replace some characters with some other characters in a string.
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!
The String object is used to manipulate a stored piece of text.
Examples of use:
The following example uses the length property of the String object to find the length of a string:
var txt="Hello world!" |
The code above will result in the following output:
12 |
The following example uses the toUpperCase() method of the String object to convert a string to uppercase letters:
var txt="Hello world!" |
The code above will result in the following output:
HELLO WORLD! |
The Date object is used to work with dates and times.
Return today's date and time
How to use the Date() method to get today's date.
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!
The Date object is used to work with dates and times.
We define a Date object with the new keyword. The following code line defines a Date object called myDate:
var myDate=new Date() |
Note: The Date object will automatically hold the current date and time as its initial value!
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):
var myDate=new Date() |
And in the following example we set a Date object to be 5 days into the future:
var myDate=new Date() |
Note: If adding five days to a date shifts the month or year, the changes are handled automatically by the Date object itself!
The Date object is also used to compare two dates.
The following example compares today's date with the 14th January 2010:
var myDate=new Date() var today = new Date() if (myDate>today) |
The Array object is used to store a set of values in a single variable name.
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.
Join two arrays - concat()
How to use the concat() method to join two arrays.
Put array elements into a string - join()
How to use the join() method to put all the elements of an array into a string.
Literal array - sort()
How to use the sort() method to sort a literal array.
Numeric array - sort()
How to use the sort() method to sort a numeric 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!
The Array object is used to store a set of values in a single variable name.
We define an Array object with the new keyword. The following code line defines an Array object called myArray:
var myArray=new 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:
var mycars=new Array() |
You could also pass an integer argument to control the array's size:
var mycars=new Array(3) |
2:
var mycars=new Array("Saab","Volvo","BMW") |
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.
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.
The following code line:
document.write(mycars[0]) |
will result in the following output:
Saab |
To modify a value in an existing array, just add a new value to the array with a specified index number:
mycars[0]="Opel" |
Now, the following code line:
document.write(mycars[0]) |
will result in the following output:
Opel
The Boolean object is used to convert a non-Boolean value to a Boolean value (true or false).
Check Boolean value
Check if a Boolean object is true or false.
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!
The Boolean object is an object wrapper for a Boolean value.
The Boolean object is used to convert a non-Boolean value to a Boolean value (true or false).
We define a Boolean object with the new keyword. The following code line defines a Boolean object called myBoolean:
var myBoolean=new Boolean() |
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:
var myBoolean=new Boolean() |
And all the following lines of code create Boolean objects with an initial value of true:
var myBoolean=new Boolean(true) |
The Math object allows you to perform common mathematical tasks.
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!
The Math object allows you to perform common mathematical tasks.
The Math object includes several mathematical values and functions. You do not need to define the Math object before using it.
JavaScript provides eight mathematical values (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 values from your JavaScript like this:
Math.E |
In addition to the mathematical values that can be accessed from the Math object there are also several functions (methods) available.
Examples of functions (methods):
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 code above will result in the following output:
5 |
The following example uses the random() method of the Math object to return a random number between 0 and 1:
document.write(Math.random()) |
The code above can result in the following output:
document.write(Math.random()) 0.5300708076970763 |
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)) |
The code above can result in the following output:
document.write(Math.floor(Math.random()*11)) 5 |
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.
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:
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 |