Welcome to the machine

Basic Javascript

The Javascript language is a computer language like no other. With its many intuitive native terms, it is a perfect introduction to object-orientated programming for the beginner, and its power allows for a great of deal of satisfaction with the expert. There is serverside Javascript, which can be used to generate HTML, including clientside script, and there is clientside script, which runs in a ‘host’ application such as a webbrowser to provide the environment.

Object types

Javascript recognizes seven types of objects, all of which have distinctive properties and methods. These objects are, in no particular order:

ArrayA numbered collection of values
var a = new Array( 'Bach', 'Zappa' );
BooleanOne bit, true or false, 1 or 0, on or off
var b = ( 2===3  );
alert( typeof b ); // boolean/**/
DateA calendar and clock
alert( new Date() );/**/
MathA calculator
alert( Math.PI + Math.cos( 0 ) );/**/
ObjectA generic placeholder for custom characteristics
var o = new Object(); o.color = 'red';
alert( o.color );/**/
RegExpA describer of text patterns
var b = new RegExp( '\\b(\\w+)\\b\\s+\\1', 'g' ).test( 'This is is no good.' );
StringA chain of characters
var s = 'Allélouya';

The typeof operator however, will return one of only six: "boolean", "number," "string", "object", "function" and "undefined". This discrepancy is due to Array, Date, Math, and RegExp actually being instances of the Object object, but special cases providing the magic of a calendar, calculator and database through their predefined properties and methods. While ar = new Array() is a perfectly legal and normal statement, even mad = new Math() results in an error: Object doesn't support this action, IE complains.

The actions that are supported, are listed on this page.


Date Properties


Date Methods

Date()Returns a Date object.
getDate()Returns the date (1-31).
getDay()Returns the day (0-6. 0=Sunday, 1=Monday, etc.).
getMonth()Returns the month (0-11. 0=January, 1=February, etc.).
getFullYear()Returns the year (four digits).
getYear()Returns the year (0-99). Use getFullYear() instead!.
getHours()Returns the hour (0-23).
getMinutes()Returns the minute (0-59).
getSeconds()Returns the second (0-59).
getMilliseconds()Returns the millisecond (0-999).
getTime()Returns the number of milliseconds since midnight 1 Jan. 1970.
getTimezoneOffset()Returns the difference between the local computer and GMT.
getUTCDate()Returns the date in universal (UTC) time.
getUTCDay()Returns the day in universal time.
getUTCMonth()Returns the month in universal time.
getUTCFullYear()Returns the four-digit year in universal time.
getUTCHours()Returns the hour in universal time.
getUTCMinutes()Returns the minutes in universal time.
getUTCSeconds()Returns the seconds in universal time.
getUTCMilliseconds()Returns the milliseconds in universal time.
parse(s)Converts string s to the number of milliseconds since midnight 1 Jan. 1970.
setDate()Sets the date of the month (1-31).
setFullYear()Sets the year (four digits).
setHours()Sets the hour (0-23).
setMilliseconds()Sets the millisecond (0-999).
setMinutes()Sets the minute (0-59).
setMonth()Sets the month (0-11. 0=January, 1=February, etc.).
setSeconds()Sets the second (0-59).
setTime()Sets the milliseconds after 1 Jan. 1970.
setYear()Sets the year (00-99).
setUTCDate()Sets the date, in universal time (1-31).
setUTCDay()Sets the day, in universal time (0-6. Sunday=0, Monday=1, etc.).
setUTCMonth()Sets the month, in universal time (0-11. 0=January, 1=February, etc.).
setUTCFullYear()Sets the year, in universal time (four digits).
setUTCHours()Sets the hour, in universal time (0-23).
setUTCMinutes()Sets the minutes, in universal time (0-59).
setUTCSeconds()Sets the seconds, in universal time (0-59).
setUTCMilliseconds()Sets the milliseconds, in universal time (0-999).
toGMTString()Converts the Date object to a string, set to GMT time zone.
toLocaleString()Converts the Date object to a string, set to the current time zone.
toString()Converts the Date object to a string.


Math Properties

EReturns the base of a natural logarithm, 2.718281828459045.
LN2Returns the natural logarithm of 2, 0.6931471805599453.
LN10Returns the natural logarithm of 10, 2.302585092994046.
LOG2EReturns the base-2 logarithm of E, 1.4426950408889633.
LOG10EReturns the base-10 logarithm of E, 0.4342944819032518.
PIReturns PI, 3.141592653589793.
SQRT1_2Returns 1 divided by the square root of 2, 0.7071067811865476.
SQRT2Returns the square root of 2, 1.4142135623730951.

Math Methods

abs(x)Returns the absolute value of x.
acos(x)Returns the arccosine of x.
asin(x)Returns the arcsine of x.
atan(x)Returns the arctangent of x.
atan2(y,x)Returns the angle from the x axis to a point.
ceil(x)Returns the nearest integer greater than or equal to x.
cos(x)Returns the cosine of x.
exp(x)Returns the value of E raised to the power of x.
floor(x)Returns the nearest integer less than or equal to x.
log(x)Returns the natural log of x.
max(x,y)Returns the number with the highest value of x and y.
min(x,y)Returns the number with the lowest value of x and y.
pow(x,y)Returns x raised to the power of y.
random()Returns a random number between 0 and 1.
round(x)Rounds x to the nearest integer.
sin(x)Returns the sine of x.
sqrt(x)Returns the square root of x.
tan(x)Returns the tangent of x.


String Properties

lengthReturns the number of characters in a string.
prototypeAllows the addition of properties to a string.

String Methods

anchor(s)Wraps text in <a name="s"> tags.
big()Wraps text in <big> tags.
blink() Wraps text in <blink> tags.
bold() Wraps text in <b> tags.
charAt(n)Returns the character at position n.
fixed() Wraps text in <tt> tags.
fontcolor(s) Wraps text in <font color="s"> tags.
fontsize(n) Wraps text in <font size="n"> tags.
indexOf(s[,x])Returns the first occurrence of string s from position x.
italics() Wraps text in <i> tags.
lastIndexOf(s[,x])Returns the last occurrence of string s from position x.
link(s) Wraps text in <a href="s"> tags.
small() Wraps text in <small> tags.
strike() Wraps text in <strike> tags.
sub() Wraps text in <sub> tags.
substring(x[,y])Returns the part from position x to y-1.
sup() Wraps text in <sup> tags.
toLowerCase()Converts all characters to lowercase.
toUpperCase()Converts all characters to uppercase.


Array Properties

constructorContains the function that created an object prototype.
lengthReturns the number of elements in the array.
prototypeAllows the addition of properties to an array.

Array Methods

concat()Combines two or more arrays into one new array.
join(s)Puts all array elements into a string separated by s (comma by default).
pop()Removes and returns the last element.
push(s1,s2,...)Adds one or more elements to the end and returns the new length.
reverse()Reverses the order of elements.
shift()Removes and returns the first element.
slice(x[,y])Creates a new array from a section of the given array.
sort()Sorts the elements alphabetically.
splice(x,n,s1,...)Adds and/or removes elements of an array.
toSource()Returns a string representing the source code of the array.
toString()Returns a string representing the array and its elements.
unshift(s1,s2,...)Adds one or more elements to the beginning and returns the new length.
valueOf()Returns the primitive value of an array.


As of JS1.5, some properties have been moved from the global RegExp object to a RegExp instance. Refer for the details.

RegExp Properties

constructorSpecifies the function that creates an object's prototype.
globalWhether to test the regular expression against all possible matches in a string, or only against the first.
ignoreCaseWhether to ignore case while attempting a match in a string. .
lastIndexThe index at which to start the next match.
multilineWhether or not to search in strings across multiple lines.
prototypeAllows the addition of properties to all objects.
sourceThe text of the pattern.

RegExp Methods

exec(s)Executes a search for a match in its string parameter.
test(s)Tests for a match in its string parameter s.
toSource()Returns an object literal representing the specified object; use this value to create a new object.
toString()Returns a string representing the specified object. Overrides the Object.toString method.


Boolean Properties

constructorContains the function that created a prototype.
prototypeAllows the addition of properties and methods.

Boolean Methods

toString()Returns a string representing the object.
valueOf()Returns a primitive value (true or false) for the object.


A generic objectlike object like no other.

Object Properties

prototypeAllows the addition of properties.

Object Methods

toSource()Returns a string representing the source code of the object.
toString()Returns a string representing the object.

Type conversion

Because converting from one type to another is sometimes as easy as adding a little +, most times even easier by simply leaving it up to the internal interpreter, because Javascript allows variables to be declared but not defined, or be redefined, because variables may exist without being of any type, the one thing all variables have in common is that some will always be more defined than others.