JavaScript Kit > JavaScript Reference > JavaScript Operators: Other
Other Operators
Below lists the other JavaScript Operators available.
Also See: JavaScript Operators precedence and associativity.
Other Operators
Operator | Description | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
(,) comma |
Primarily used in a for loop when you wish to insert multiple expressions for the test conditions, the comma (,) operator evaluates its left and right operands, and returns the value of the right.
for (var i=0, y=0; i<5; i++, y+=2){ Output:
|
||||||||||||||||
?: |
The Conditional Operator (?: ) is a shorthand method for returning the result of either exp1 or exp2, depending on whether condition evaluates to true or false. If true, the value of exp1 is returned, or if false, the value of exp2 instead.
(condition)? exp1 : exp2 For example: y=-1 |
||||||||||||||||
delete |
Deletes a variable defined not using "var ", custom object property, method, or array element. It returns true if successful, false if not. For example:
var myarray=['joe', 'mary', 'jane'] |
||||||||||||||||
in |
The "in" operator takes an object or array as its right operand, and a string denoting the property you wish to search for as its left operand. Returns true if property exists in object, false if not.
var myarray=['joe', 'mary', 'jane'] Note that properties deleted using the "delete" operator above will return false when tested for using "in". |
||||||||||||||||
instanceof |
Returns a Boolean value indicating whether the left hand operand is of the object type specified in the right hand operand. Here object type refers to the name of the object you wish to test for, such as Date, String, Array etc. Use instanceof as a more clear-cut way of detecting the type of an object or variable versus typeof . For example:
var today=new Date() |
||||||||||||||||
new |
Creates a new instance of a custom or select built-in JavaScript objects such as Date. Only objects with a constructor function can be instantialized using the "new " operator. The "this " keyword can then be used anywhere inside the function to refer to the object or object instance.
var today=new Date() //create new instance of Date object |
||||||||||||||||
try/catch/finally |
Allows you to intercept exception (aka runtime) errors in JavaScript and handle them as you see fit. Examples of exceptions include trying to reference an undefined variable, or calling a non existent JavaScript method. It does not catch syntax errors (ie: forgetting to end a string with a quotation mark). The typical set up involves a try and catch clause to catch a possible exception, and react accordingly if caught:
try{ In the above, if " There's another clause, try{
var ajaxrequest=null Here I'm using a nested More info: "Handling runtime errors in JavaScript using try/catch/finally". |
||||||||||||||||
typeof |
The "typeof" operator allows you to probe the data type of its operand, such as whether a variable is string, numeric, or even undefined. Here are some of the possible return values:
|
||||||||||||||||
void |
Evaluates a lone operand without returning a value. Most commonly used to define JavaScript links, where the link executes a JavaScript code instead of its default action when clicked on. For example:
<a href="javascript:void(window.open('http://google.com'))">Open Google</a> |
Example- Conditional Operator
The Conditional Operator is handy for quickly assigning a different value to a variable depending on the outcome of a condition. Here are a few examples:
var browser=(navigator.appName.indexOf("Microsoft")!=-1)? "IE" : "Non IE"
You can expand the number of possible values to assign to more than just two. In fact, there is no limit. Observe the below example, which has 3 possible values:
var browser=(navigator.appName.indexOf("Microsoft")!=-1)? "IE" : (navigator.appName.indexOf("Netscape")? "NS" : "Not IE nor NS"
In the 2nd example, "browser" will contain one of 3 possible values depending on the browser maker of the visitor.You can use a conditional operator within a larger expression, by wrapping the entire conditional operator with a parenthesis:
document.write("The total for your order is "+(onsale==true? "$25" : "$30"))
- JavaScript Operators
- JavaScript Statements
- Global functions
- JavaScript Events
- Escape Sequences
- Reserved Words