You are on page 1of 72

NUMBER METHODS

1. constructor() Syntax Example

Returns the function that created this object's instance. By default this is the Number object. number.constructor() var num = new Number( 177.1234 ); document.write("num.constructor() is : " + num.constructor); num.constructor() is : function Number() { [native code] } Forces a number to display in exponential notation, even if the number is in the range in which JavaScript normally uses standard notation. number.toExponential( [fractionDigits] ) var num=77.1234; var val = num.toExponential(); document.write("num.toExponential() is : " + val ); document.write("<br />"); val = num.toExponential(4); document.write("num.toExponential(4) is : " + val ); document.write("<br />"); val = num.toExponential(2); document.write("num.toExponential(2) is : " + val); document.write("<br />");

Output

2. toExponential()

Syntax Example

Output

num.toExponential() is : 7.71234e+1 num.toExponential(4) is : 7.7123e+1 num.toExponential(2) is : 7.71e+1 Formats a number with a specific number of digits to the right of the decimal. number.toFixed( [digits] ) var num=177.1234;

3. toFixed() Syntax Example

BOOLEAN METHODS
1. toSource() Returns a string containing the source of the Boolean object; you can use this string to create an equivalent object. boolean.toSource() function book(title, publisher, price) { this.title = title; this.publisher = publisher; this.price = price; } var newBook = new book("Perl","Leo Inc",200); document.write(newBook.toSource()); ({title:"Perl", publisher:"Leo Inc", price:200}) Returns a string of either "true" or "false" depending upon the value of the object. boolean.toString() Syntax Example var flag = new Boolean(false); document.write( "flag.toString is : " + flag.toString() ); flag.toString is : false Output 3. valueOf() Returns the primitive value of the Boolean object.

Syntax Example

Output

2. toString()

Syntax Example

boolean.valueOf() var flag = new Boolean(false); document.write( "flag.valueOf is : " + flag.valueOf() ); flag.valueOf is : false

Output

STRING METHODS
1. charAt() Syntax Example Returns the character at the specified index. string.charAt(index); var str = new String( "This is string" ); document.writeln("str.charAt(0) is:" + str.charAt(0)); document.writeln("<br />str.charAt(1) is:" + str.charAt(1)); document.writeln("<br />str.charAt(2) is:" + str.charAt(2)); str.charAt(0) is:T str.charAt(1) is:h str.charAt(2) is:i Returns a number indicating the Unicode value of the character at the given index. string.charCodeAt(index); var str = new String( "This is string" ); document.write("str.charCodeAt(0) is:" + str.charCodeAt(0)); document.write("<br />str.charCodeAt(1) is:" + str.charCodeAt(1)); document.write("<br />str.charCodeAt(2) is:" + str.charCodeAt(2)); document.write("<br />str.charCodeAt(3) is:" + str.charCodeAt(3)); str.charCodeAt(0) is:84 str.charCodeAt(1) is:104 str.charCodeAt(2) is:105

Output

2. charCodeAt() Syntax Example

Output

str.charCodeAt(3) is:115 3. concat() Syntax Example Combines the text of two strings and returns a new string. string.concat(string2, string3[, ..., stringN]); var str1 = new String( "This is string one" ); var str2 = new String( "This is string two" ); var str3 = str1.concat( str2 ); document.write("Concatenated String :" + str3); Concatenated String :This is string oneThis is string two. Returns the index within the calling String object of the first occurrence of the specified value, or -1 if not found. string.indexOf(searchValue[, fromIndex]) var str1 = new String( "This is string one" ); var index = str1.indexOf( "string" ); document.write("indexOf found String :" + index ); indexOf found String :8 Returns the index within the calling String object of the last occurrence of the specified value, or -1 if not found.

Output

4. indexOf()

Syntax Example

Output 5. lastIndexOf()

Syntax Example

string.lastIndexOf(searchValue[, fromIndex]) var str1 = new String( "This is string one and again string" ); var index = str1.lastIndexOf( "string" ); document.write("lastIndexOf found String :" + index ); lastIndexOf found String :29 Returns a number indicating whether a reference string comes before or after or is the same as the given string in sort order. string.localeCompare( param ) var str1 = new String( "This is beautiful string" ); var index = str1.localeCompare( "XYZ" ); document.write("localeCompare first :" + index ); document.write("<br />" ); var index = str1.localeCompare( "AbCD ?" ); document.write("localeCompare second :" + index );

Output 6. localeCompare()

Syntax Example

Output

localeCompare first :-1 localeCompare second :1 Returns the length of the string.

7. length()

Syntax Example

string.length var str = new String( "This is string" ); document.write("str.length is:" + str.length); str.length is:14 Used to match a regular expression against a string. string.match( param ) var str = "For more information, see Chapter 3.4.5.1"; var re = /(chapter \d+(\.\d)*)/i; var found = str.match( re ); document.write(found );

Output 8. match() Syntax Example

Output 9. replace()

Chapter 3.4.5.1,Chapter 3.4.5.1,.1 Used to find a match between a regular expression and a string, and to replace the matched substring with a new substring. string.replace(regexp/substr, newSubStr/function[, flags]); var var are var re = /apples/gi; str = "Apples are round, and apples juicy."; newstr = str.replace(re, "oranges");

Syntax Example

document.write(newstr );

oranges are round, and oranges are juicy. Output Example var re = /(\w+)\s(\w+)/; var str = "zara ali"; var newstr = str.replace(re, "$2, $1"); document.write(newstr); ali, zara Executes the search for a match between a regular expression and a specified string. string.search(regexp); var re = /apples/gi; var str = "Apples are round, and apples are juicy."; if ( str.search(re) == 0 ){ document.write("Does not contain Apples" ); }else{ document.write("Contains Apples" ); } Output 11 slice() . Syntax Example Does not contain Apples Extracts a section of a string and returns a new string. string.slice( beginslice [, endSlice] ); var str = "Apples are round, and apples are juicy.";

Output 10 search() . Syntax Example

var sliced = str.slice(4, -3); document.write( sliced ); Output 12 split() . Syntax Example es are round, and apples are jui Splits a String object into an array of strings by separating the string into substrings. string.split([separator][, limit]); var str = "Apples are round, and apples are juicy."; var splitted = str.split(" ", 4); document.write( splitted ); Apples,are,round,, and Returns the characters in a string beginning at the specified location through the specified number of characters. string.substr(start[, length]); var str = "Apples are round, and apples are juicy."; document.write("(1,2): " + str.substr(1,2)); document.write("<br />(-2,2): " + str.substr(-2,2)); document.write("<br />(1): " + str.substr(1));

Output 13 substr() . Syntax Example

document.write("<br />(20, 2): " + str.substr(20,2)); Output (1,2): pp (-2,2): Ap (1): pples are round, and apples are juicy. (20, 2): d Returns the characters in a string between two indexes into the string. string.substring(indexA, [indexB]) var str = "Apples are round, and apples are juicy."; document.write("(1,2): " + str.substring(1,2)); document.write("<br />(0,10): " + str.substring(0, 10)); document.write("<br />(5): " + str.substring(5)); Output (1,2): p (0,10): Apples are (5): s are round, and apples are juicy.

14 substring() . Syntax Example

15 toLocaleLowerCase() The characters within a string are . converted to lower case while respecting the current locale. Syntax Example string.toLocaleLowerCase( ) var str = "Apples are round, and Apples are Juicy.";

document.write(str.toLocaleLowerCase( )) ; Output apples are round, and apples are juicy.

16 toLocaleUpperCase() The characters within a string are . converted to upper case while respecting the current locale. Syntax Example string.toLocaleUpperCase( ) var str = "Apples are round, and Apples are Juicy."; document.write(str.toLocaleUpperCase( )) ; Output APPLES ARE ROUND, AND APPLES ARE JUICY. Returns the calling string value converted to lower case. string.toLowerCase( ) var str = "Apples are round, and Apples are Juicy."; document.write(str.toLowerCase( )); Output 18 toString() . Syntax apples are round, and apples are juicy. Returns a string representing the specified object. string.toString( )

17 toLowerCase() . Syntax Example

Example

var str = "Apples are round, and Apples are Juicy."; document.write(str.toString( ));

Output 19 toUpperCase() . Syntax Example

Apples are round, and Apples are Juicy. Returns the calling string value converted to uppercase. string.toUpperCase( ) var str = "Apples are round, and Apples are Juicy."; document.write(str.toUpperCase( ));

Output

APPLES ARE ROUND, AND APPLES ARE JUICY. Returns the primitive value of the specified object. string.valueOf( ) var str = new String("Hello world"); document.write(str.valueOf( ));

20 valueOf() . Syntax Example

Output

Hello world

STRING HTML WRAPPERS


1. anchor() Syntax Example Creates an HTML anchor that is used as a hypertext target. string.anchor( anchorname ) var str = new String("Hello world"); alert(str.anchor( "myanchor" )); Output 2. big() Syntax Example <a name="myanchor">Hello world</a> Creates a string to be displayed in a big font as if it were in a <big> tag. string.big( ) var str = new String("Hello world"); alert(str.big()); Output 3. blink() Syntax Example <big>Hello world</big> Creates a string to blink as if it were in a <blink> tag. string.blink( ) var str = new String("Hello world"); alert(str.blink()); Output <blink>Hello world</blink>

4. bold() Syntax Example

Creates a string to be displayed as bold as if it were in a <b> tag. string.bold( ) var str = new String("Hello world"); alert(str.bold());

Output 5. fixed() Syntax Example

<b>Hello world</b> Causes a string to be displayed in fixedpitch font as if it were in a <tt> tag string.fixed( ) var str = new String("Hello world"); alert(str.fixed());

Output 6. fontcolor()

<tt>Hello world</tt> Causes a string to be displayed in the specified color as if it were in a <font color="color"> tag. string.fontcolor( color ) var str = new String("Hello world"); alert(str.fontcolor( "red" ));

Syntax Example

Output 7. fontsize()

<font color="red">Hello world</font> Causes a string to be displayed in the specified font size as if it were in a <font

size="size"> tag. Syntax Example string.fontsize( size ) var str = new String("Hello world"); alert(str.fontsize( 3 )); Output 8. italics() Syntax Example <font size="3">Hello world</font> Causes a string to be italic, as if it were in an <i> tag. string.italics( ) var str = new String("Hello world"); alert(str.italics( 3 )); Output 9. link() Syntax Example <i>Hello world</i> Creates an HTML hypertext link that requests another URL. string.link( hrefname ) var str = new String("Hello world"); var URL = "http://www.tutorialspoint.com"; alert(str.link( URL )); Output <a href="http://www.tutorialspoint.com">H ello world</a>

10 small() . Syntax Example Output 11 strike() . Syntax Example Output 12 sub() . Syntax Example Output 13 sup() . Syntax

Causes a string to be displayed in a small font, as if it were in a <small> tag. string.small( ) var str = new String("Hello world"); alert(str.small()); <small>Hello world</small> Causes a string to be displayed as struck-out text, as if it were in a <strike> tag. string.strike( ) var str = new String("Hello world"); alert(str.strike()); <strike>Hello world</strike> Causes a string to be displayed as a subscript, as if it were in a <sub> tag string.sub( ) var str = new String("Hello world"); alert(str.sub()); <sub>Hello world</sub> Causes a string to be displayed as a superscript, as if it were in a <sup> tag string.sup( )

Example Output

var str = new String("Hello world"); alert(str.sup()); <sup>Hello world</sup>

ARRAY METHODS
1. concat() Returns a new array comprised of this array joined with other array(s) and/or value(s). array.concat(value1, value2, ..., valueN); var alpha = ["a", "b", "c"]; var numeric = [1, 2, 3]; var alphaNumeric = alpha.concat(numeric); document.write("alphaNumeric : " + alphaNumeric ); Output 2. every() alphaNumeric : a,b,c,1,2,3 Returns true if every element in this array satisfies the provided testing function. array.every(callback[, thisObject]); if (!Array.prototype.every) { Array.prototype.every =

Syntax Example

Syntax Example

function(fun /*, thisp*/) { var len = this.length; if (typeof fun != "function") throw new TypeError(); var thisp = arguments[1]; for (var i = 0; i < len; i++) { if (i in this && !fun.call(thisp, this[i], i, this)) return false; } return true; }; } function isBigEnough(element, index, array) { return (element >= 10); } var passed = [12, 5, 8, 130, 44].every(isBigEnough); document.write("First Test Value : " + passed ); passed = [12, 54, 18, 130, 44].every(isBigEnough); document.write("Second Test Value : " + passed ); Output First Test Value : falseSecond Test Value : true Creates a new array with all of the elements of this array for which the

3. filter()

provided filtering function returns true. Syntax Example array.filter(callback[, thisObject]); if (!Array.prototype.filter) { Array.prototype.filter = function(fun /*, thisp*/) { var len = this.length; if (typeof fun != "function") throw new TypeError(); var res = new Array(); var thisp = arguments[1]; for (var i = 0; i < len; i++) { if (i in this) { var val = this[i]; // in case fun mutates this if (fun.call(thisp, val, i, this)) res.push(val); } } return res; }; } function isBigEnough(element, index, array) { return (element >= 10); } var filtered = [12, 5, 8, 130, 44].filter(isBigEnough);

document.write("Filtered Value : " + filtered ); Output 4. forEach() Syntax Example Filtered Value : 12,130,44 Calls a function for each element in the array. array.forEach(callback[, thisObject]); if (!Array.prototype.forEach) { Array.prototype.forEach = function(fun /*, thisp*/) { var len = this.length; if (typeof fun != "function") throw new TypeError(); var thisp = arguments[1]; for (var i = 0; i < len; i++) { if (i in this) fun.call(thisp, this[i], i, this); } }; } function printBr(element, index, array) { document.write("<br />[" + index + "] is " + element ); } [12, 5, 8, 130, 44].forEach(printBr); Output [0] is 12 [1] is 5

[2] is 8 [3] is 130 [4] is 44 5. indexOf() Returns the first (least) index of an element within the array equal to the specified value, or -1 if none is found. array.indexOf(searchElement[, fromIndex]); if (!Array.prototype.indexOf) { Array.prototype.indexOf = function(elt /*, from*/) { var len = this.length; var from = Number(arguments[1]) || 0; from = (from < 0) ? Math.ceil(from) : Math.floor(from); if (from < 0) from += len; for (; from < len; from++) { if (from in this && this[from] === elt) return from; } return -1; }; } var index = [12, 5, 8, 130, 44].indexOf(8);

Syntax Example

document.write("index is : " + index ); var index = [12, 5, 8, 130, 44].indexOf(13); document.write("<br />index is : " + index ); Output index is : 2 index is : -1 Joins all elements of an array into a string. array.join(separator); var arr = new Array("First","Second","Third"); var str = arr.join(); document.write("str : " + str ); var str = arr.join(", "); document.write("<br />str : " + str ); var str = arr.join(" + "); document.write("<br />str : " + str ); Output str : First,Second,Third str : First, Second, Third str : First + Second + Third Returns the last (greatest) index of an element within the array equal to the specified value, or -1 if none is found. array.lastIndexOf(searchElement[,

6. join() Syntax Example

7. lastIndexOf()

Syntax

fromIndex]); Example if (!Array.prototype.lastIndexOf) { Array.prototype.lastIndexOf = function(elt /*, from*/) { var len = this.length; var from = Number(arguments[1]); if (isNaN(from)) { from = len - 1; } else { from = (from < 0) ? Math.ceil(from) : Math.floor(from); if (from < 0) from += len; else if (from >= len) from = len - 1; } for (; from > -1; from--) { if (from in this && this[from] === elt) return from; } return -1; }; } var index = [12, 5, 8, 130, 44].lastIndexOf(8);

document.write("index is : " + index ); var index = [12, 5, 8, 130, 44, 5].lastIndexOf(5); document.write("<br />index is : " + index ); Output index is : 2 index is : 5 Creates a new array with the results of calling a provided function on every element in this array. array.map(callback[, thisObject]); if (!Array.prototype.map) { Array.prototype.map = function(fun /*, thisp*/) { var len = this.length; if (typeof fun != "function") throw new TypeError(); var res = new Array(len); var thisp = arguments[1]; for (var i = 0; i < len; i++) { if (i in this) res[i] = fun.call(thisp, this[i], i, this); } return res; }; }

8. map()

Syntax Example

var numbers = [1, 4, 9]; var roots = numbers.map(Math.sqrt); document.write("roots is : " + roots ); Output 9. pop() Syntax Example roots is : 1,2,3 Removes the last element from an array and returns that element. array.pop(); var numbers = [1, 4, 9]; var element = numbers.pop(); document.write("element is : " + element ); var element = numbers.pop(); document.write("<br />element is : " + element ); Output element is : 9 element is : 4 Adds one or more elements to the end of an array and returns the new length of the array. array.push(element1, ..., elementN); var numbers = new Array(1, 4, 9); var length = numbers.push(10); document.write("new numbers is : " +

10 push() . Syntax Example

numbers ); length = numbers.push(20); document.write("<br />new numbers is : " + numbers ); Output new numbers is : 1,4,9,10 new numbers is : 1,4,9,10,20 Apply a function simultaneously against two values of the array (from left-toright) as to reduce it to a single value. array.reduce(callback[, initialValue]); if (!Array.prototype.reduce) { Array.prototype.reduce = function(fun /*, initial*/) { var len = this.length; if (typeof fun != "function") throw new TypeError(); // no value to return if no initial value and an empty array if (len == 0 && arguments.length == 1) throw new TypeError(); var i = 0; if (arguments.length >= 2) { var rv = arguments[1]; } else {

11 reduce() . Syntax Example

do { if (i in this) { rv = this[i++]; break; } // if array contains no values, no initial value to return if (++i >= len) throw new TypeError(); } while (true); } for (; i < len; i++) { if (i in this) rv = fun.call(null, rv, this[i], i, this); } return rv; }; } var total = [0, 1, 2, 3].reduce(function(a, b){ return a + b; }); document.write("total is : " + total ); Output 12 reduceRight() . total is : 6 Apply a function simultaneously against two values of the array (from right-toleft) as to reduce it to a single value.

Syntax Example

array.reduceRight(callback[, initialValue]); if (!Array.prototype.reduceRight) { Array.prototype.reduceRight = function(fun /*, initial*/) { var len = this.length; if (typeof fun != "function") throw new TypeError(); // no value to return if no initial value, empty array if (len == 0 && arguments.length == 1) throw new TypeError(); var i = len - 1; if (arguments.length >= 2) { var rv = arguments[1]; } else { do { if (i in this) { rv = this[i--]; break; } // if array contains no values, no initial value to return if (--i < 0)

throw new TypeError(); } while (true); } for (; i >= 0; i--) { if (i in this) rv = fun.call(null, rv, this[i], i, this); } return rv; }; } var total = [0, 1, 2, 3].reduceRight(function(a, b) { return a + b; }); document.write("total is : " + total ); Output 13 reverse() . Syntax Example total is : 6 Reverses the order of the elements of an array -- the first becomes the last, and the last becomes the first. array.reverse(); var arr = [0, 1, 2, 3].reverse(); document.write("Reversed array is : " + arr ); Reversed array is : 3,2,1,0

Output

14 shift() . Syntax Example

Removes the first element from an array and returns that element. array.shift(); var element = [105, 1, 2, 3].shift(); document.write("Removed element is : " + element ); Removed element is : 105 Extracts a section of an array and returns a new array. array.slice( begin [,end] ); var arr = ["orange", "mango", "banana", "sugar", "tea"]; document.write("arr.slice( 1, 2) : " + arr.slice( 1, 2) ); document.write("<br />arr.slice( 1, 3) : " + arr.slice( 1, 3) ); arr.slice( 1, 2) : mango arr.slice( 1, 3) : mango,banana Returns true if at least one element in this array satisfies the provided testing function. array.some(callback[, thisObject]); if (!Array.prototype.some) { Array.prototype.some = function(fun /*, thisp*/)

Output 15 slice() . Syntax Example

Output

16 some() . Syntax Example

{ var len = this.length; if (typeof fun != "function") throw new TypeError(); var thisp = arguments[1]; for (var i = 0; i < len; i++) { if (i in this && fun.call(thisp, this[i], i, this)) return true; } return false; }; } function isBigEnough(element, index, array) { return (element >= 10); } var retval = [2, 5, 8, 1, 4].some(isBigEnough); document.write("Returned value is : " + retval ); var retval = [12, 5, 8, 1, 4].some(isBigEnough); document.write("<br />Returned value is : " + retval ); Output Returned value is : false Returned value is : true Represents the source code of an object

17 toSource() .

Syntax Example

array.toSource(); var arr = new Array("orange", "mango", "banana", "sugar"); var str = arr.toSource(); document.write("Returned string is : " + str ) ;

Output

Returned string is : ["orange", "mango", "banana", "sugar"] Sorts the elements of an array. array.sort( compareFunction ); var arr = new Array("orange", "mango", "banana", "sugar"); var sorted = arr.sort(); document.write("Returned string is : " + sorted );

18 sort() . Syntax Example

Output

Returned array is : banana,mango,orange,sugar Adds and/or removes elements from an array. array.splice(index, howMany, [element1][, ..., elementN]); var arr = ["orange", "mango", "banana", "sugar", "tea"]; var removed = arr.splice(2, 0, "water");

19 splice() . Syntax Example

document.write("After adding 1: " + arr ); document.write("<br />removed is: " + removed); removed = arr.splice(3, 1); document.write("<br />After adding 1: " + arr ); document.write("<br />removed is: " + removed); Output After adding 1: orange,mango,water,banana,sugar,tea removed is: After adding 1: orange,mango,water,sugar,tea removed is: banana Returns a string representing the array and its elements. array.toString(); var arr = new Array("orange", "mango", "banana", "sugar"); var str = arr.toString(); document.write("Returned string is : " + str ); Output Returned string is : orange,mango,banana,sugar Adds one or more elements to the front of an array and returns the new length of the array.

20 toString() . Syntax Example

21 unshift() .

Syntax Example

array.unshift( element1, ..., elementN ); var arr = new Array("orange", "mango", "banana", "sugar"); var length = arr.unshift("water"); document.write("Returned array is : " + arr ); document.write("<br /> Length of the array is : " + length );

Output

Returned array is : water,orange,mango,banana,sugar Length of the array is : 5

DATE METHODS
1. Date() Syntax Example Returns today's date and time Date() var dt = Date(); document.write("Date and Time : " + dt ); Date and Time : Wed Aug 27 2008 20:12:50 GMT+0530 (India Standard Time) Returns the day of the month for the specified date according to local time. Date.getDate() var dt = new Date("December 25, 1995 23:15:00"); document.write("getDate() : " + dt.getDate() ); getDate() : 25 Returns the day of the week for the specified date according to local time. Date.getDay() var dt = new Date("December 25, 1995 23:15:00"); document.write("getDay() : " +

Output

2. getDate() Syntax Example

Output 3. getDay() Syntax Example

dt.getDay() ); Output 4. getFullYear() Syntax Example getDay() : 1 Returns the year of the specified date according to local time. Date.getFullYear() var dt = new Date("December 25, 1995 23:15:00"); document.write("getFullYear() : " + dt.getFullYear() ); getFullYear() : 1995 Returns the hour in the specified date according to local time. Date.getHours() var dt = new Date("December 25, 1995 23:15:00"); document.write("getHours() : " + dt.getHours() ); getHours() : 23 Returns the milliseconds in the specified date according to local time. Date.getMilliseconds() var dt = new Date( ); document.write("getMilliseconds() : " + dt.getMilliseconds() );

Output 5. getHours() Syntax Example

Output 6. getMilliseconds() Syntax Example

Output 7. getMinutes() Syntax Example

getMilliseconds() : 578 Returns the minutes in the specified date according to local time. Date.getMinutes() var dt = new Date( "December 25, 1995 23:15:00" ); document.write("getMinutes() : " + dt.getMinutes() ); getMinutes() : 15 Returns the month in the specified date according to local time. Date.getMonth() var dt = new Date( "December 25, 1995 23:15:00" ); document.write("getMonth() : " + dt.getMonth() ); getMonth() : 11 Returns the seconds in the specified date according to local time. Date.getSeconds() var dt = new Date( "December 25, 1995 23:15:20" ); document.write("getSeconds() : " + dt.getSeconds() );

Output 8. getMonth() Syntax Example

Output 9. getSeconds() Syntax Example

Output 10 getTime() .

getSeconds() : 20 Returns the numeric value of the specified date as the number of milliseconds since January 1, 1970, 00:00:00 UTC. Date.getTime() var dt = new Date( "December 25, 1995 23:15:20" ); document.write("getTime() : " + dt.getTime() ); getTime() : 819913520000

Syntax Example

Output

11 getTimezoneOffset() Returns the time-zone offset in minutes . for the current locale. Syntax Example Date.getTimezoneOffset() var dt = new Date(); var tz = dt.getTimezoneOffset(); document.write("getTimezoneOffset() : " + tz ); getTimezoneOffset() : -330 Returns the day (date) of the month in the specified date according to universal time. Date.getUTCDate() var dt = new Date( "December 25, 1995

Output 12 getUTCDate() . Syntax Example

23:15:20" ); document.write("getUTCDate() : " + dt.getUTCDate() ); Output 13 getUTCDay() . Syntax Example getUTCDate() : 25 Returns the day of the week in the specified date according to universal time. Date.getUTCDay() var dt = new Date( "December 25, 1995 23:15:20" ); document.write("getUTCDay() : " + dt.getUTCDay() ); getUTCDay() : 1 Returns the year in the specified date according to universal time. Date.getUTCFullYear() var dt = new Date( "December 25, 1995 23:15:20" ); document.write("getUTCFullYear() : " + dt.getUTCFullYear() ); getUTCFullYear() : 1995 Returns the hours in the specified date according to universal time. Date.getUTCHours()

Output 14 getUTCFullYear() . Syntax Example

Output 15 getUTCHours() . Syntax

Example

var dt = new Date(); document.write("getUTCHours() : " + dt.getUTCHours() ); getUTCHours() : 14

Output

16 getUTCMilliseconds() Returns the milliseconds in the specified . date according to universal time. Syntax Example Date.getUTCMilliseconds() var dt = new Date(); document.write("getUTCMilliseconds() : " + dt.getUTCMilliseconds() ); getUTCMilliseconds() : 453 Returns the minutes in the specified date according to universal time. Date.getUTCMinutes() var dt = new Date(); document.write("getUTCMinutes() : " + dt.getUTCMinutes() ); getUTCMinutes() : 32 Returns the month in the specified date according to universal time. Date.getUTCMonth() var dt = new Date(); document.write("getUTCMonth() : " + dt.getUTCMonth() );

Output 17 getUTCMinutes() . Syntax Example

Output 18 getUTCMonth() . Syntax Example

Output 19 getUTCSeconds() . Syntax Example

getUTCMonth() : 7 Returns the seconds in the specified date according to universal time. Date.getUTCSeconds() var dt = new Date(); document.write("getUTCSeconds() : " + dt.getUTCSeconds() ); getUTCSeconds() : 14 Deprecated - Returns the year in the specified date according to local time. Use getFullYear instead. Date.getYear() var dt = new Date(); document.write("getYear() : " + dt.getYear() ); getYear() : 208 Sets the day of the month for a specified date according to local time. Date.setDate( dayValue ) var dt = new Date( "Aug 28, 2008 23:30:00" ); dt.setDate( 24 ); document.write( dt );

Output 20 getYear() . Syntax Example

Output 21 setDate() . Syntax Example

Output 22 setFullYear() . Syntax Example

Sun Aug 24 23:30:00 UTC+0530 2008 Sets the full year for a specified date according to local time. Date.setFullYear(yearValue[, monthValue[, dayValue]]) var dt = new Date( "Aug 28, 2008 23:30:00" ); dt.setFullYear( 2000 ); document.write( dt ); Mon Aug 28 23:30:00 UTC+0530 2000 Sets the hours for a specified date according to local time. Date.setHours(hoursValue[, minutesValue[,secondsValue[, msValue]]]) var dt = new Date( "Aug 28, 2008 23:30:00" ); dt.setHours( 02 ); document.write( dt );

Output 23 setHours() . Syntax Example

Output 24 setMilliseconds() . Syntax Example

Thu Aug 28 02:30:00 UTC+0530 2008 Sets the milliseconds for a specified date according to local time. Date.setMilliseconds(millisecondsValue) var dt = new Date( "Aug 28, 2008 23:30:00" );

dt.setMilliseconds( 1010 ); document.write( dt ); Output 25 setMinutes() . Syntax Example Thu Aug 28 23:30:01 UTC+0530 2008 Sets the minutes for a specified date according to local time. Date.setMinutes(minutesValue[, secondsValue[, msValue]]) var dt = new Date( "Aug 28, 2008 23:30:00" ); dt.setMinutes( 45 ); document.write( dt ); Thu Aug 28 23:45:00 UTC+0530 2008 Sets the month for a specified date according to local time. Date.setMonth(monthValue[, dayValue]) var dt = new Date( "Aug 28, 2008 23:30:00" ); dt.setMonth( 2 ); document.write( dt ); Fri Mar 28 23:30:00 UTC+0530 2008 Sets the seconds for a specified date according to local time. Date.setSeconds(secondsValue[, msValue])

Output 26 setMonth() . Syntax Example

Output 27 setSeconds() . Syntax Example

var dt = new Date( "Aug 28, 2008 23:30:00" ); dt.setSeconds( 80 ); document.write( dt ); Output 28 setTime() . Syntax Example Thu Aug 28 23:31:20 UTC+0530 2008 Sets the Date object to the time represented by a number of milliseconds since January 1, 1970, 00:00:00 UTC. Date.setTime(timeValue) var dt = new Date( "Aug 28, 2008 23:30:00" ); dt.setTime( 5000000 ); document.write( dt ); Thu Jan 1 06:53:20 UTC+0530 1970 Sets the day of the month for a specified date according to universal time. Date.setUTCDate(dayValue) Syntax Example var dt = new Date( "Aug 28, 2008 23:30:00" ); dt.setUTCDate( 20 ); document.write( dt ); Wed Aug 20 23:30:00 UTC+0530 2008 Output 30 setUTCFullYear() . Syntax Sets the full year for a specified date according to universal time. Date.setUTCFullYear(yearValue[,

Output 29 setUTCDate() .

monthValue[, dayValue]]) Example var dt = new Date( "Aug 28, 2008 23:30:00" ); dt.setUTCFullYear( 2006 ); document.write( dt ); Mon Aug 28 23:30:00 UTC+0530 2006 Sets the hour for a specified date according to universal time. Date.setUTCHours(hoursValue[, minutesValue[,secondsValue[, msValue]]]) var dt = new Date( "Aug 28, 2008 23:30:00" ); dt.setUTCHours( 15 ); document.write( dt ); Output Thu Aug 28 20:30:00 UTC+0530 2008

Output 31 setUTCHours() . Syntax Example

32 setUTCMilliseconds() Sets the milliseconds for a specified date . according to universal time. Syntax Example Date.setUTCMilliseconds( millisecondsVa lue ) var dt = new Date( "Aug 28, 2008 23:30:00" ); dt.setUTCMilliseconds( 1100 ); document.write( dt ); Thu Aug 28 23:30:01 UTC+0530 2008

Output

33 setUTCMinutes() . Syntax Example

Sets the minutes for a specified date according to universal time. Date.setUTCMinutes(minutesValue[, secondsValue[, msValue]]) var dt = new Date( "Aug 28, 2008 13:30:00" ); dt.setUTCMinutes( 65 ); document.write( dt ); Thu Aug 28 14:35:00 UTC+0530 2008 Sets the month for a specified date according to universal time. Sets the seconds for a specified date according to universal time. Date.setUTCSeconds(secondsValue[, msValue]) var dt = new Date( "Aug 28, 2008 13:30:00" ); dt.setUTCSeconds( 65 ); document.write( dt ); Thu Aug 28 13:31:05 UTC+0530 2008 Deprecated - Sets the year for a specified date according to local time. Use setFullYear instead. Date.setYear(yearValue) var dt = new Date( "Aug 28, 2008

Output 34 setUTCMonth() . 35 setUTCSeconds() . Syntax Example

Output 36 setYear() . Syntax Example

13:30:00" ); dt.setYear( 2000 ); document.write( dt ); Output 37 toDateString() . Syntax Example Mon Aug 28 13:30:00 UTC+0530 2000 Returns the "date" portion of the Date as a human-readable string. Date.toDateString() var dt = new Date(1993, 6, 28, 14, 39, 7); document.write( "Formated Date : " + dt.toDateString() ); Formated Date : Wed Jul 28 1993 Deprecated - Converts a date to a string, using the Internet GMT conventions. Use toUTCString instead. Date.toGMTString() var dt = new Date(1993, 6, 28, 14, 39, 7); document.write( "Formated Date : " + dt.toGMTString() ); Formated Date : Wed, 28 Jul 1993 09:09:07 UTC

Output 38 toGMTString() . Syntax Example

Output

39 toLocaleDateString() Returns the "date" portion of the Date . as a string, using the current locale's conventions.

Syntax Example

Date.toLocaleDateString() var dt = new Date(1993, 6, 28, 14, 39, 7); document.write( "Formated Date : " + dt.toLocaleDateString() ); Formated Date : Wednesday, July 28, 1993 Converts a date to a string, using a format string. Date.toLocaleFormat(formatString); var dt = new Date(1993, 6, 28, 14, 39, 7); document.write( "Formated Date : "+ dt.toLocaleFormat( "%A, %B %e, %Y" ) ); Formated Date : Wednesday, July , 1993 Converts a date to a string, using the current locale's conventions. Date.toLocaleString(); var dt = new Date(1993, 6, 28, 14, 39, 7); document.write( "Formated Date : " + dt.toLocaleString() ); Formated Date : Wednesday, July 28, 1993 2:39:07 PM

Output

40 toLocaleFormat() . Syntax Example

Output 41 toLocaleString() . Syntax Example

Output

42 toLocaleTimeString() Returns the "time" portion of the Date . as a string, using the current locale's conventions. Syntax Example Date.toLocaleTimeString() var dt = new Date(1993, 6, 28, 14, 39, 7); document.write( "Formated Date : " + dt.toLocaleTimeString() ); Formated Date : 2:39:07 PM Returns a string representing the source for an equivalent Date object; you can use this value to create a new object. Date.toSource() var dt = new Date(1993, 6, 28, 14, 39, 7); document.write( "Formated Date : " + dt.toSource() ); Formated Date : (new Date(743850547000)) Returns a string representing the specified Date object. Date.toString() var dateobject = new Date(1993, 6, 28, 14, 39, 7); stringobj = dateobject.toString();

Output 43 toSource() . Syntax Example

Output

44 toString() . Syntax Example

document.write( "String Object : " + stringobj ); Output String Object : Wed Jul 28 14:39:07 UTC+0530 1993 Returns the "time" portion of the Date as a human-readable string. Date.toTimeString() var dateobject = new Date(1993, 6, 28, 14, 39, 7); document.write( dateobject.toTimeStri ng() ); 14:39:07 UTC+0530 Converts a date to a string, using the universal time convention. Date.toUTCString() var dateobject = new Date(1993, 6, 28, 14, 39, 7); document.write( dateobject.toUTCStri ng() ); Wed, 28 Jul 1993 09:09:07 UTC Returns the primitive value of a Date object. Date.valueOf() var dateobject = new Date(1993, 6,

45 toTimeString() . Syntax Example

Output 46 toUTCString() . Syntax Example

Output 47 valueOf() . Syntax Example

28, 14, 39, 7); document.write( dateobject.valueOf() ); Output 743850547000

MATH METHODS
1. abs() Syntax Example Returns the absolute value of a number. Math.abs( x ) ; var value = Math.abs(-1); document.write("First Test Value : " + value ); var value = Math.abs(null); document.write("<br />Second Test Value : " + value ); var value = Math.abs(20); document.write("<br />Third Test Value : " + value ); var value = Math.abs("string"); document.write("<br />Fourth Test Value : " + value ); Output First Test Value : 1 Second Test Value : 0 Third Test Value : 20 Fourth Test Value : NaN Returns the arccosine (in radians) of a number. Math.acos( x ) ; var value = Math.acos(-1); document.write("First Test Value : " + value );

2. acos() Syntax Example

var value = Math.acos(null); document.write("<br />Second Test Value : " + value ); var value = Math.acos(30); document.write("<br />Third Test Value : " + value ); var value = Math.acos("string"); document.write("<br />Fourth Test Value : " + value ); Output First Test Value : 3.141592653589793 Second Test Value : 1.5707963267948965 Third Test Value : NaN Fourth Test Value : NaN Returns the arcsine (in radians) of a number. Math.asin( x ) ; var value = Math.asin(-1); document.write("First Test Value : " + value ); var value = Math.asin(null); document.write("<br />Second Test Value : " + value ); var value = Math.asin(30); document.write("<br />Third Test Value : " + value ); var value = Math.asin("string"); document.write("<br />Fourth Test

3. asin() Syntax Example

Value : " + value ); Output First Test Value : -1.5707963267948965 Second Test Value : 0 Third Test Value : NaN Fourth Test Value : NaN Returns the arctangent (in radians) of a number. Math.atan( x ) ; var value = Math.atan(-1); document.write("First Test Value : " + value ); var value = Math.atan(.5); document.write("<br />Second Test Value : " + value ); var value = Math.atan(30); document.write("<br />Third Test Value : " + value ); var value = Math.atan("string"); document.write("<br />Fourth Test Value : " + value ); Output First Test Value : -0.7853981633974483 Second Test Value : 0.4636476090008061 Third Test Value : 1.5374753309166493 Fourth Test Value : NaN Returns the arctangent of the quotient of its arguments.

4. atan() Syntax Example

5. atan2()

Syntax Example

Math.atan2( x, y ) ; var value = Math.atan2(90,15); document.write("First Test Value : " + value ); var value = Math.atan2(15,90); document.write("<br />Second Test Value : " + value ); var value = Math.atan2(0, -0); document.write("<br />Third Test Value : " + value ); var value = Math.atan2(+Infinity, -Infinity); document.write("<br />Fourth Test Value : " + value );

Output

First Test Value : 1.4056476493802698 Second Test Value : 0.16514867741462683 Third Test Value : 3.141592653589793 Fourth Test Value : 2.356194490192345 Returns the smallest integer greater than or equal to a number. Math.ceil( x ) ; var value = Math.ceil(45.95); document.write("First Test Value : " + value ); var value = Math.ceil(45.20); document.write("<br />Second Test

6. ceil() Syntax Example

Value : " + value ); var value = Math.ceil(-45.95); document.write("<br />Third Test Value : " + value ); var value = Math.ceil(-45.20); document.write("<br />Fourth Test Value : " + value ); Output First Test Value : 46 Second Test Value : 46 Third Test Value : -45 Fourth Test Value : -45 Returns the cosine of a number. Math.cos( x ) ; var value = Math.cos(90); document.write("First Test Value : " + value ); var value = Math.cos(30); document.write("<br />Second Test Value : " + value ); var value = Math.cos(-1); document.write("<br />Third Test Value : " + value ); var value = Math.cos(2*Math.PI); document.write("<br />Fourth Test Value : " + value ); Output First Test Value : -0.4480736161291701 Second Test Value :

7. cos() Syntax Example

0.15425144988758405 Third Test Value : 0.5403023058681398 Fourth Test Value : 1 8. exp() Returns EN, where N is the argument, and E is Euler's constant, the base of the natural logarithm. Math.exp( x ) ; var value = Math.exp(1); document.write("First Test Value : " + value ); var value = Math.exp(30); document.write("<br />Second Test Value : " + value ); var value = Math.exp(-1); document.write("<br />Third Test Value : " + value ); var value = Math.exp(.5); document.write("<br />Fourth Test Value : " + value ); Output First Test Value : 2.718281828459045 Second Test Value : 10686474581524.462 Third Test Value : 0.36787944117144233 Fourth Test Value : 1.6487212707001282 Returns the largest integer less than or equal to a number.

Syntax Example

9. floor()

Syntax Example

Math.floor( x ) ; var value = Math.floor(10.3); document.write("First Test Value : " + value ); var value = Math.floor(30.9); document.write("<br />Second Test Value : " + value ); var value = Math.floor(-2.9); document.write("<br />Third Test Value : " + value ); var value = Math.floor(-2.2); document.write("<br />Fourth Test Value : " + value );

Output

First Test Value : 10 Second Test Value : 30 Third Test Value : -3 Fourth Test Value : -3 Returns the natural logarithm (base E) of a number. Math.log( x ) ; var value = Math.log(10); document.write("First Test Value : " + value ); var value = Math.log(0); document.write("<br />Second Test Value : " + value );

10 log() . Syntax Example

var value = Math.log(-1); document.write("<br />Third Test Value : " + value ); var value = Math.log(100); document.write("<br />Fourth Test Value : " + value ); Output First Test Value : 2.302585092994046 Second Test Value : -Infinity Third Test Value : NaN Fourth Test Value : 4.605170185988092 Returns the largest of zero or more numbers. Math.max(value1, value2, ... valueN ) ; var value = Math.max(10, 20, -1, 100); document.write("First Test Value : " + value ); var value = Math.max(-1, -3, -40); document.write("<br />Second Test Value : " + value ); var value = Math.max(0, -1); document.write("<br />Third Test Value : " + value ); var value = Math.max(100); document.write("<br />Fourth Test Value : " + value ); Output First Test Value : 100 Second Test Value : -1 Third Test Value : 0

11 max() . Syntax Example

Fourth Test Value : 100 12 min() . Syntax Example Returns the smallest of zero or more numbers. Math.min(value1, value2, ... valueN ) ; var value = Math.min(10, 20, -1, 100); document.write("First Test Value : " + value ); var value = Math.min(-1, -3, -40); document.write("<br />Second Test Value : " + value ); var value = Math.min(0, -1); document.write("<br />Third Test Value : " + value ); var value = Math.min(100); document.write("<br />Fourth Test Value : " + value ); Output First Test Value : -1 Second Test Value : -40 Third Test Value : -1 Fourth Test Value : 100 Returns base to the exponent power, that is, base exponent. Math.pow(base, exponent ) ; var value = Math.pow(7, 2); document.write("First Test Value : " + value );

13 pow() . Syntax Example

var value = Math.pow(8, 8); document.write("<br />Second Test Value : " + value ); var value = Math.pow(-1, 2); document.write("<br />Third Test Value : " + value ); var value = Math.pow(0, 10); document.write("<br />Fourth Test Value : " + value ); Output First Test Value : 49 Second Test Value : 16777216 Third Test Value : 1 Fourth Test Value : 0 Returns a pseudo-random number between 0 and 1. Math.random() ; var value = Math.random( ); document.write("First Test Value : " + value ); var value = Math.random( ); document.write("<br />Second Test Value : " + value ); var value = Math.random( ); document.write("<br />Third Test Value : " + value ); var value = Math.random( ); document.write("<br />Fourth Test

14 random() . Syntax Example

Value : " + value ); Output First Test Value : 0.28182050319352636 Second Test Value : 0.6375786089661319 Third Test Value : 0.6780129774072902 Fourth Test Value : 0.7310958163154001 Returns the value of a number rounded to the nearest integer. Math.round( x ) ; var value = Math.round( 0.5 ); document.write("First Test Value : " + value ); var value = Math.round( 20.7 ); document.write("<br />Second Test Value : " + value ); var value = Math.round( 20.3 ); document.write("<br />Third Test Value : " + value ); var value = Math.round( -20.3 ); document.write("<br />Fourth Test Value : " + value ); Output First Test Value : 1 Second Test Value : 21 Third Test Value : 20 Fourth Test Value : -20

15 round() . Syntax Example

16 sin() . Syntax Example

Returns the sine of a number. Math.sin( x ) ; var value = Math.sin( 0.5 ); document.write("First Test Value : " + value ); var value = Math.sin( 90 ); document.write("<br />Second Test Value : " + value ); var value = Math.sin( 1 ); document.write("<br />Third Test Value : " + value ); var value = Math.sin( Math.PI/2 ); document.write("<br />Fourth Test Value : " + value );

Output

First Test Value : 0.479425538604203 Second Test Value : 0.8939966636005578 Third Test Value : 0.8414709848078965 Fourth Test Value : 1 Returns the square root of a number. Math.sqrt( x ) ; var value = Math.sqrt( 0.5 ); document.write("First Test Value : " + value ); var value = Math.sqrt( 81 ); document.write("<br />Second Test Value : " + value );

17 sqrt() . Syntax Example

var value = Math.sqrt( 13 ); document.write("<br />Third Test Value : " + value ); var value = Math.sqrt( -4 ); document.write("<br />Fourth Test Value : " + value ); Output First Test Value : 0.7071067811865476 Second Test Value : 9 Third Test Value : 3.605551275463989 Fourth Test Value : NaN Returns the tangent of a number. Math.tan( x ) ; var value = Math.tan( -30 ); document.write("First Test Value : " + value ); var value = Math.tan( 90 ); document.write("<br />Second Test Value : " + value ); var value = Math.tan( 45 ); document.write("<br />Third Test Value : " + value ); var value = Math.tan( Math.PI/180 ); document.write("<br />Fourth Test Value : " + value ); Output First Test Value : 1 Second Test Value : 21 Third Test Value : 20 Fourth Test Value : -20

18 tan() . Syntax Example

19 toSource() . Syntax Example Output

Returns the string "Math". Math.toSource() ; var value = Math.toSource( ); document.write("Value : " + value ); Value : Math

Escaping \ Escapes special characters to literal and literal characters to special. E.g: /\(s\)/ matches '(s)' while /(\s)/ matches any whitespace and captures the match. Quantifiers {n}, {n,}, Quantifiers match the preceding subpattern a {n,m}, *, +, ? certain number of times. The subpattern can be a single character, an escape sequence, a pattern enclosed by parentheses or a character set. {n} matches exactly n times. {n,} matches n or more times. {n,m} matches n to m times. * is short for {0,}. Matches zero or more times. + is short for {1,}. Matches one or more times. ? is short for {0,1}. Matches zero or one time. E.g: /o{1,3}/ matches 'oo' in "tooth" and 'o' in "nose". Pattern delimiters (pattern), (?:pattern) Matches entire contained pattern. (pattern) captures match. (?:pattern) doesn't capture match E.g: /(d).\1/ matches and captures 'dad' in "abcdadef" while /(?:.d){2}/ matches but doesn't capture 'cdad'. Note: (?:pattern) is a JavaScript 1.5 feature. Lookaheads

(?=pattern), (?!pattern)

A lookahead matches only if the preceding subexpression is followed by the pattern, but the pattern is not part of the match. The subexpression is the part of the regular expression which will be matched. (?=pattern) matches only if there is a following pattern in input. (?!pattern) matches only if there is not a following pattern in input. E.g: /Win(?=98)/ matches 'Win' only if 'Win' is followed by '98'. Note: Lookahead is a JavaScript1.5 feature.

Alternation | Alternation matches content on either side of the alternation character. E.g: /(a|b)a/ matches 'aa' in "dseaas" and 'ba' in "acbab". Character sets [characters], Matches any of the contained characters. A range [^characters] of characters may be defined by using a hyphen. [characters] matches any of the contained characters. [^characters] negates the character set and matches all but the contained characters E.g: /[abcd]/ matches any of the characters 'a', 'b', 'c', 'd' and may be abbreviated to /[a-d]/. Ranges must be in ascending order, otherwise they will throw an error. (E.g: /[d-a]/ will throw an error.) /[^0-9]/ matches all characters but digits.

Note: Most special characters are automatically escaped to their literal meaning in character sets. Special characters ^, $, ., ? and all the highlighted characters above in the table. Special characters are characters that match something else than what they appear as. ^ matches beginning of input (or new line with m flag). $ matches end of input (or end of line with m flag). . matches any character except a newline. ? directly following a quantifier makes the quantifier non-greedy (makes it match minimum instead of maximum of the interval defined). E.g: /(.)*?/ matches nothing or '' in all strings. Note: Non-greedy matches are not supported in older browsers such as Netscape Navigator 4 or Microsoft Internet Explorer 5.0. Literal characters All characters except those with special meaning. \n Mapped directly to the corresponding character. E.g: /a/ matches 'a' in "Any ancestor".

Backreferences Backreferences are references to the same thing as a previously captured match. n is a positive nonzero integer telling the browser which captured match to reference to. /(\S)\1(\1)+/g matches all occurrences of three equal non-whitespace characters following each other.

/<(\S+).*>(.*)<\/\1>/ matches any tag. E.g: /<(\S+).*>(.*)<\/\1>/ matches '<div id="me">text</div>' in "text<div id=\"me\">text</div>text". Character Escapes \f, \r, \n, \t, \v, \0, [\b], \s, \S, \w, \W, \d, \D, \b, \B, \cX, \xhh, \uhhhh \f matches form-feed. \r matches carriage return. \n matches linefeed. \t matches horizontal tab. \v matches vertical tab. \0 matches NUL character. [\b] matches backspace. \s matches whitespace (short for [\f\n\r\t\v\u00A0\u2028\u2029]). \S matches anything but a whitespace (short for [^\f\n\r\t\v\u00A0\u2028\u2029]). \w matches any alphanumerical character (word characters) including underscore (short for [a-zAZ0-9_]). \W matches any non-word characters (short for [^a-zA-Z0-9_]). \d matches any digit (short for [0-9]). \D matches any non-digit (short for [^0-9]). \b matches a word boundary (the position between a word and a space). \B matches a non-word boundary (short for [^\b]). \cX matches a control character. E.g: \cm matches control-M. \xhh matches the character with two characters of hexadecimal code hh. \uhhhh matches the Unicode character with four characters of hexadecimal code hhhh.

You might also like