JavaScript » Special

?: Operator


This is a conditional operator that takes three operands and is used to replace simple if statements. The first operand is a condition that evaluates to true or false, the second is an expression of any type to be returned if the condition is true, and third is an expression of any type to be returned if the condition is false.

Examples

Code:
with(beer)
document.write("This beer is " + ((percent_proof < 5) ? "mild" : "strong"));
Explanation:

The above code displays one of two messages depending on the value of the object property 'percent_proof'.


, Operator


This is the comma operator which is most often used to include multiple expressions where only one is required, particularly in a for loop. It evaluates both its operands and returns the value of the second.

Examples

Code:
for(var i=0, j=0; i<3; i++, j++)
document.write(a[i][j] + "<BR>");
Explanation:

The above code uses a two-dimensional array of 3 by 3 elements and initializes two counters (one for each dimension) incrementing them both, which results in a display of the left-to-right diagonal values.

Code:
for(var i=0, j=0; i<3; i++, j++)
document.write(a[i, j] + "<BR>");
Explanation:

while using the comma operator inside the square brackets of the above example causes the code to display all the elements of the array one row at a time.

delete Operator

The delete operator is used to delete an object, an object's property or a specified element in an array, returning true if the operation is possible, and false if not.

Examples

Code:
fruit = new Object;
fruit.name = 'apple';
fruit.color = 'green';
fruit.size = 'large';
delete fruit.size;
with(fruit)
delete color;
delete fruit;
Explanation:

With the defined object 'fruit', the above delete operations are possible.

NOTE:

To delete an object's property, you must precede that property's name with the name of the object, unless it's used in a with statement.
The delete operator can also be used to delete an element of an array. This does not affect the length of the array or any of the other elements but changes the deleted element to undefined.

Examples

Code:
fruit = new Array ("apple", "pear", "orange", "cherry", "grape");
delete fruit[2];
Explanation:

The above example creates an array called 'fruit' and then deletes element #2 (orange).

new Operator


The new operator can be used to create an instance of a user-defined object type or of one of the built-in object types that has a constructor function. To create a user-defined object type you must first define it by writing a function that specifies its name, properties and methods.

Examples

Code:
function book(title, category, author)
{
this.title = title
this.category = category
this.author = author
}
Explanation:

The above function creates an object for books with properties for title, category and author.

Code:
mybook = new book("The Thing", "horror", "John Lynch")
Explanation:

You can than create an instance of the object as in the above example which assigns the values "The Thing", "horror" and "John Lynch" to the respective properties.

Code:
function author(name, real_name, age)
{
this.name = name
this.real_name = real_name
this.age = age
}
author1 = new author("John Lynch", "Charlie Schwarz", 43)
Explanation:

The property of an object can even be another object. You could, for example, create an object for authors and use it in the above example of the 'book' object. Again you would first define it by writing a function, and then you could create an instance of it for "John Lynch" as above.

Code:
document.write(mybook.author.age)
Explanation:

Now, as long as the 'author' object and the relevent instance of it have already been defined, the 'author' property in the 'book' object will refer to it, and you can display, say, the age of the author of 'mybook' as above.

Code:
mybook.publisher = "Centurion Books"
Explanation:

You can also add a property to any instance of an object as in the above example which adds the property 'publisher' to the 'mybook' object and assigns it the value "Centurion Books".

Code:
book.prototype.publisher = null
mybook.publisher = "Centurion Books"
Explanation:

If, however, you wanted to add a property to a previously defined object type (and, therefore, all instances of it) you would have to use the prototype property. This above example adds the property 'publisher' to the 'book' object type, and then assigns the value 'Centurion Books' to one particular instance of it.

this Operator


The keyword this is used to refer to the current object. In a method, it usually refers to the calling object. In the following example the code first creates a function DescribeAge that takes as its parameter an object, and returns one of two text values depending on the value of that same object's Year property. Then another object called Car is created whose third property Description is the value returned by the DescribeAge function.

Examples

Code:
function describeAge(obj)
{
if(obj.year < 1996)
return "Old-fashioned"
else
return "Good-as-new"
}
function car(make, year, description)
{this.make = make, this.year = year, this.description = describeAge(this)}
myCar = new car("Ford", "1993", describeAge(this))
Explanation:

The keyword this is used as the parameter of the DescribeAge function to refer to whichever object is calling it, as seen in the final bit of code which creates a specific instance of the Car object whose Description property will now contain the string "Old-fashioned".

typeof Operator


The typeof operator returns the type of an unevaluated operand which can be a number, string, variable, object or keyword.

Examples

Code:
typeof(age) returns number
typeof 33 returns number
Explanation:

It can be used with or without brackets as in the above examples of a numeric literal and the variable 'age' being 60.

The following values are returned for various types of data:

Code:
a number returns 'number';
a string returns 'string';
the keyword true returns 'boolean';
the keyword null returns 'object';
methods, functions and predefined objects return 'function';
a variable returns the type of the data assigned to it;
a property returns the type of its value;

void Operator


The void operator evaluates an expression without returning a value. Although the use of brackets after it is optional, it is good style to use them.

Examples

Code:
Sam turned green.
Explanation:

The above example creates a hyperlink on the word "green" which, when clicked, changes the background color to light green.