JavaScript has 7 special operators:
The conditional operator takes three operands. It can be used wherever you would use a standard operator. The operator can have one of two values depending on a condition. If the condition is true, the operator takes the first value. If the condition is false, the operator takes the second value. The syntax is:
condition ? value1 : value2.
For example, a dish is cooked after at least 45 minutes:
Dish = (time >= 45) ? "dish cooked" : "dish uncooked"The comma operator (,) evaluates both of its operands and returns the value of the second. It is generally used inside a for loop, allowing multiple variables to be updated each time through the loop.
For example, if a is a 2-dimensional array with 6 elements per side, the following code uses the comma operator to increment two variables at once. The code prints the values of the 6 diagonal elements in the array:
for
(var row=0, column=5; row <= 5; row++, column--)
document.writeln("a["+row+","+column+"]= " + a[row,column])
Back to top
With the delete operator you can:
delete objectNamedelete objectName.propertydelete objectName[index]delete property (legal only in a with statement)where:
objectName is the name of an objectproperty is an existing propertyindex is an integer representing the location of an element in an arrayThe delete operator can delete variables declared implicitly, but not those declared with the var statement. The delete operator sets the property or element to undefined and returns true if it succeeds. It returns false if the operation is not possible. Examples:
a=64var b= 12345mynumb=new Number()mynumb.x=4 // creates property xdelete a // returns true(succeeds as declared implicitly)delete b // returns false(fails as declared with var)delete Math.PI // returns false(cannot delete predefined properties)delete mynumb.x // returns true(can delete user-defined properties)delete mynumb // returns true(can delete user-defined object)
xrefer to 'with' statement?
Use the delete operator to delete an array element without affecting the array length. In the following example, deleting letters[4] leaves letters[4] undefined (not in the array) and leaves letters[5] as it is.
letters=new Array("A","B","C","D","E","F")
delete letters[4]
if (letters[4]) {
document.write("4 in letters") // this does not get executed
}
if (letters[5]) {
document.write("5 in letters") // this does get executed
document.write(letters[4]) // 'undefined' printed
document.write(letters[5]) // 'F' printed
}
original test 'if 4 in letters' does not work. Find out how to check.
For an array element to exist but have an undefined value, use the undefined keyword instead of the delete operator. For example:
letters=new Array("A","B","C","D","E","F")
letters[4]=undefined
if (letters[4]) {
document.write("4 in letters") // this does not get executed
}
if (letters[5]) {
document.write("5 in letters") // this does get executed
document.write(letters[4]) // 'undefined' printed
document.write(letters[5]) // 'F' printed
}
Back to top
The new operator is used to create an instance of one of the predefined object types Array, Boolean, Date, Function, Image, Number, Object, Option, RegExp, or String, or one of your own user-defined object types. On the server, you can also create instances of DbPool, File, Lock, or SendMail. The syntax is:
objectName = new objectType ( param1 [,param2] ...[,paramN] )
You can also create objects using object initializers, as described in "Using Object Initializers" on page 101. See new in the Client-Side JavaScript Reference for more information.
Back to topThe this keyword is used to refer to the current object, generally the calling object in a method. The syntax is:
this[.propertyName]
Example 1. Suppose a function called check checks an object's year property, given the object and the high and low values:
function check(obj, lowyear, hiyear) {
if ((obj.year < lowyear) || (obj.year > hiyear))
alert("Invalid Year!")
}
You could call check in each form element's onChange event handler, using this to pass it the form element, as in the following example:
Enter date of birth:
<INPUT TYPE = "text" NAME = "year" SIZE = 4
onChange="check(this, 1890, 1999)">
untested - need to create environment
This can refer to the current object's parent form.
Example 2. When combined with the form property, this can refer to the current object's parent form. In the following example, the form myForm contains a Text object and a button. When the user clicks the button, the value of the Text object is set to the form's name. The button's onClick event handler uses this.form to refer to the parent form, myForm.
<FORM NAME="myForm">
Form name:<INPUT TYPE="text" NAME="text1" VALUE="Beluga">
<INPUT NAME="button1" TYPE="button" VALUE="Show Form Name"
onClick="this.form.text1.value=this.form.name">
</FORM>
Back to top
The typeof operator returns a string indicating the type of the unevaluated operand (string, variable, keyword, or object). The parentheses are optional. Syntax:
typeof operand or typeof (operand)
For example:
var newFun = new Function("9+8")
typeof newFun is object // IE & NN say function
var topic="Algebra"
typeof topic is string
var age=99
typeof age is number
var birthday new Date()
typeof birthday is object
typeof nosuchThing is undefined
typeof true is boolean // true keyword
typeof null is object // null keyword
typeof 55 is number // number
typeof 'Jabberwock' is string // string
typeof document.lastModified is string // property
typeof window.length is number // property
typeof Math.LN2 is number // property
typeof blur is function // methods & functions // IE: object
typeof eval is function // methods & functions
typeof parseInt is function // methods & functions
typeof shape.split is function // methods & functions
typeof Date is function // predefined object
typeof Function is function // predefined object
typeof Math is function // predefined object - IE,NN say Math
typeof Option is function // predefined object - IE says object
typeof String is function // predefined object
replace some of the examples with my own
Back to topThe void operator specifies a JavaScript expression to be evaluated without returning a value. Parentheses are optional, but preferred.
Syntax:
void (expression) or void expression
One use is to specify an expression as a hypertext link. The expression is evaluated but is not loaded in place of the current document. void(0) evaluates to 0, but that has no effect in JavaScript.
<A HREF="javascript:void(0)">If you click here, nothing happens</A>
To create a hypertext link that submits a form when the user clicks it:
<A HREF="javascript:void(document.form.submit())">Click here to submit</A>
Add own examples
Back to top Home Serious Stuff
© 2001. Copyright Sue Nethercott.