Special operators (JavaScript)
A number of special operators are available.
Operator | Description |
---|---|
condition ? expr1 : expr2 |
Returns the value of expr1 if condition is
true, or the value of expr2 if condition is
false. This operator can be used as a shortcut for the if statement. |
expr1 , expr2 |
Evaluates both expressions and returns the value
of expr2 . This operator can be used to put multiple
expressions in one statement, typically var and for . |
delete objectName |
Deletes objectName . Returns
true if the deletion succeeds; otherwise, returns false. Attempting
to access a deleted object causes a runtime error. Objects include
variables. |
delete objectName.property |
Deletes a user-defined property of objectName (not
a predefined property). Sets the property value to undefined and returns
true if the deletion succeeds; otherwise, returns false. |
delete property |
Same as delete objectName.property but
is used as part of a with statement. |
delete objectName[index] |
Do not use. To "delete" an element of an array,
assign the value undefined to the array element. |
function(param, param, ...) { statements
} |
Declares an anonymous function in an expression. |
function name(param, param, ...) { statements
} |
Declares a named function in an expression. |
objectName = new objectType(param, param,
...) |
Creates an object based on a built-in or user-defined object type. |
this |
Refers to the object that initiated execution.
If you call a function, this inside the function
refers to the calling object. If you create an object based on a function, this inside
the function refers to the function. |
this.propertyName |
Refers to a property (variable) of the object that initiated execution. |
typeof operand |
Returns a string indicating the type of the
operand. The operand can be an object, property, variable, or literal.
Common types are number , string , boolean , object , function ,
and null . |
typeof(operand) |
Same as typeof operand . |
void expression |
Evaluates an expression without returning a value. |
void(expression) |
Same as void expression . |
object.property |
Refers to an object property (variable). The property name must be a valid identifier. |
object["property"]
|
Refers to an object property (variable). The property name can be any string. |
Usage
To define an object type, create a function for the object type that specifies its name, properties, and methods. An object can have a property that is itself another object.The in
and instanceof
operators
are not supported server-side.
Examples
This example prints one string or another depending on a condition.function p(stuff) {
print("<<<" + stuff + ">>>");
}
// Test true path
var n = 5;
p(n > 0 ? "n is greater than 0" : "n is not greater than 0");
// Test false path
n = -1;
p(n > 0 ? "n is greater than 0" : "n is not greater than 0");
// Same as ...
if(n > 0)
p("n is greater than 0");
else
p("n is not greater than 0");
This example shows three comma scenarios. The first scenario is not useful, but the latter two are.
function p(stuff) {
print("<<<" + stuff + ">>>");
}
p(0, -1, 1); // prints 1
var n = 0, m = -1, o = 1;
p(n); p(m); p(o); // prints 0 -1 1
for(var i = 0, j = 1; i < 5; i++, j++) {
p("i = " + i + " and j = " + j);
} // prints i = 0-4 and j = 1-5
This example deletes a Number object. The attempt to access it after deletion results in a runtime error.
function p(stuff) {
print("<<<" + stuff + ">>>");
}
try {
var num = new Number(2.4);
p(num.toFixed(8));
if(delete num) p("num deleted"); else p("num not deleted");
p(num.toFixed(8));
} catch(e) {
p(e);
}
This example creates a property for a Number object then deletes it.
function p(stuff) {
print("<<<" + stuff + ">>>");
}
try {
var num = new Number(2.4);
p(num.toFixed(8));
num.myprop = 5;
p(num.myprop);
if(delete num.myprop) p("num.myprop deleted");
else p("num.myprop not deleted");
p(num.toFixed(8));
if(num.myprop == undefined) p("undefined"); else ("notundefined");
} catch(e) {
p(e);
}
This example demonstrates creating and calling an anonymous function.
function p(stuff) {
print("<<<" + stuff + ">>>");
}
foo = function(param1, param2, param3) {
p("param1 = " + param1);
p("param2 = " + param2);
p("param3 = " + param3);
}
foo(1, 2, 3);
This example demonstrates creating and calling a named function by its assigned variable and by its name.
function p(stuff) {
print("<<<" + stuff + ">>>");
}
foo = function bar(param1, param2, param3) {
p("param1 = " + param1);
p("param2 = " + param2);
p("param3 = " + param3);
}
foo(1, 2, 3);
bar(1, 2, 3);
This example shows how to create objects for built-in and user-defined types.
function p(stuff) {
print("<<<" + stuff + ">>>");
}
n = new Number(2.4);
p("n = " + n.toFixed(8));
// Create function for user-defined object
// Here "this" means the user-defined object
function myobj(param1, param2, param3) {
this.prop1 = param1;
this.prop2 = param2;
this.prop3 = param3;
}
m = new myobj(1, 2, 3);
p("m.prop1 = " + m.prop1);
p("m.prop2 = " + m.prop2);
p("m.prop3 = " + m.prop3);
This example demonstrates dot and bracket notation for properties.
function p(stuff) {
print("<<<" + stuff + ">>>");
}
function f() {
this.s = "DOT_S";
}
var ff = new f();
p("ff.s = " + ff.s); // <<<ff.s = DOT_S>>>
p("ff[\"s\"] = " + ff["s"]); // <<<ff["s"] = DOT_S>>>