Object Objects

To create an object the new expression is used:

var myObj = new Object();
This object has the attribute of length and the method toString(). Other attributes and functions can be added
myObj.address = "123 Any Street";
myObj.state = "TX";
myObj.func = myFunc;  // myFunc is the name of some function

You can also write a function as a constructor and then use that to create objects.

function student (name, major, gpa)
{
  this.name = name;
  this.major = major;
  this.gpa = gpa;
}

var someStudent = new student ("Mickey Mouse", "Dance", 3.84);

String Objects

A String object is created by using the String constructor in a new expression and supplying a string as an argument.

var name = new String ("John Doe");

A String object can be coerced to a string value by calling the String constructor as a function. When String is called without the new expression then it acts as a function and performs type conversions. For example:

var ten = String ( 10 );

Returns a string value (not a String object) computed by the toString() method.

Attribute Name Description
length Returns the number of characters in a string
 
Method Name Description
toString() Returns this string value. Same as valueOf().
valueOf() Returns this string value.
charAt (pos) Returns a string containing the character at the position pos. If there is no character at that position an empty string is returned.
concat (str1, str2, ...) Returns a string consisting of the characters of this object (converted to a string) followed by the characters of each of the strings str1, str2, and so on.
indexOf (searchStr, pos) Returns the position of the first occurrence of the search string starting at the position pos. If it does not find the search string then -1 is returned. If pos is not specified then it is assumed to be 0.
lastIndexOf (searchStr, pos) Returns the position of the last occurrence of the search string before the position pos and -1 if its does not find it. If pos is not specified then it is assumed to be 0.
match (regexp) This method is similar to indexOf() and lastIndexOf(). Instead of returning the position it returns the specified string. This method is case sensitive.
replace (searchVal, replaceVal) This method replaces the characters in searchVal with the characters in replaceVal. This method is case sensitive.
search (regexp) This returns the position of the first occurrence of a match of the regular expression in the string or -1 if there is no match.
slice (start, end) Returns a string starting from the character at the position start through but not including the character at position end. If end is not specified then all the characters to the end of the original string is returned.
split (separator, limit) Returns an array of substrings. The separator is a character, regular expression, or substring that is used to determine where to split the string. limit is an optional numerical parameter that specifies how many times the split should occur.
substring (start, end) Returns a substring starting from the character at the position start through but not including the character at position end. If end is not specified the all the characters to the end of the string is returned.
toLowerCase() Returns a string that has all the characters of the original string in lower case.
toUpperCase() Returns a string that has all the characters of the original string in upper case.

Math Objects

The Math object has attribues and provides methods for basic mathematical functions. To call upon an attribute or method use the name of the object Math followed by the dot operator and then the name of the attribute or function - Math.PI or Math.sqrt(). The convention is similar to that in Java where Math is the name of the class and all its methods are static, so that one does not need to create an object to access the methods.

Attribute Name Description
E Base of natural logarithm e = 2.7182818284590452354
LN10 Natural logarithm of 10 = 2.302585092994046
LN2 Natural logarithm of 2 = 0.6931471805599453
LOG2E Logarithm of e in base 2 = 1.4426950408889634
LOG10E Logarithm of e in base 10 = 0.4342944819032518
PI Value of π = 3.1415926535897932
SQRT1_2 Square root of (1/2) = 0.7071067811865476
SQRT2 Square root of 2 = 1.4142135623730951
 
Function Name Description
abs (x) Returns the absolute value of x.
acos (x) Returns the arc cosine of x expressed in radians in the range +0 to +π.
asin (x) Returns the arc sine of x expressed in radians in the range -π/2 to +π/2.
atan (x) Returns the arc tangent of x expressed in radians in the range -π/2 to +π/2.
atan2 (y, x) Returns the arc tangent of the quotient of y/x in radians in the range -π to +π.
ceil (x) Returns the smallest number value that is not less than x and is equal to a mathematical integer.
cos (x) Returns the cosine of x where x is in radians.
exp (x) Returns e raised to the power x.
floor (x) returns the greatest number value that is not greater than x and is equal to a mathematical integer.
log (x) Returns the natural logarithm of x.
max (val1, val2, ...) Returns the largest of the values.
min (val1, val2, ...) Returns the minimum of the values.
pow (x, y) Retuns the result of raising x to the power y.
random () Returns a number greater than or equal to 0 but less than 1.
round (x) Returns the number value that is closest to x and is equal to a mathematical integer.
sin (x) Returns the sine of x, where x is in radians.
sqrt (x) Returns the square root of x.
tan (x) Returns the tangent of x, where x is in radians.

Date Objects

The Date object contains a number indicating a particular instant of time to within one millisecond. Time is measured in milliseconds since 01 January 1970 UTC. Leap seconds are ignored.

There are several constructors of the Date object but in its simplest form a Date object is created using the new operator.

var today = new Date();
Method Description
toLocaleString() Returns a string representation of the date and time in the current time zone.
toLocaleDateString() Returns a string representation of the date portion in the current time zone.
toLocaleTimeString() Returns a string representation of the time portion in the current time zone.
getDate() Returns the day of the month (1 - 31).
getDay() Returns the day of the week (0 - 6).
getMonth() Returns the month (0 - 11).
getFullYear() Returns the year as a 4 digit number.
getHours() Returns the hour (0 - 23).
getMinutes() Returns the minutes (0 - 59).
getSeconds() Returns the seconds (0 - 59).
getTime() Returns the number of milliseconds since midnight 01 Jan 1970.
getTimezoneOffset() Returns the difference in minutes between local time and Greenwich Mean Time (GMT).
parse() Takes a date string and returns the number of milliseconds since midnight of 01 Jan 1970.
setDate() Sets the day of the month (1 - 31).
setMonth() Sets the month (0 -11).
setFullYear() Sets the year (4 digits).
setHours() Sets the hour (0 - 23).
setMinutes() Sets the minutes (0 - 59).
setSeconds() Set the seconds (0 - 59).
setTime() Calculates a date and time by adding or subtracting a specified number of milliseconds to / from midnight 01 Jan 1970.