Note: many of the contents of this page are taken from w3school website.
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!" document.write(txt.length)
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!" document.write(txt.toUpperCase())
The code above will result in the following output:
HELLO WORLD!
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 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() myDate.setFullYear(2010,0,14)
And in the following example we set a Date object to be 5 days into the future:
var myDate=new Date() myDate.setDate(myDate.getDate()+5)
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()
myDate.setFullYear(2010,0,14)
var today = new Date()
if (myDate>today)
alert("Today is before 14th January 2010")
else
alert("Today is after 14th January 2010")
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 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() mycars[0]="Saab" mycars[1]="Volvo" mycars[2]="BMW"
You could also pass an integer argument to control the array's size:
var mycars=new Array(3) mycars[0]="Saab" mycars[1]="Volvo" mycars[2]="BMW"
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
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 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()
var myBoolean=new Boolean(0)
var myBoolean=new Boolean(null)
var myBoolean=new Boolean("")
var myBoolean=new Boolean(false)
var myBoolean=new Boolean(NaN)
And all the following lines of code create Boolean objects with an initial value of true:
var myBoolean=new Boolean(true)
var myBoolean=new Boolean("true")
var myBoolean=new Boolean("false")
var myBoolean=new Boolean("Richard")
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 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 Math.PI Math.SQRT2 Math.SQRT1_2 Math.LN2 Math.LN10 Math.LOG2E Math.LOG10E
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:
0.13251054050929345
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:
3
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 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 |