Online Documentation Server
 ПОИСК
ods.com.ua Web
 КАТЕГОРИИ
Home
Programming
Net technology
Unixes
Security
RFC, HOWTO
Web technology
Data bases
Other docs

 


 ПОДПИСКА

 О КОПИРАЙТАХ
Вся предоставленная на этом сервере информация собрана нами из разных источников. Если Вам кажется, что публикация каких-то документов нарушает чьи-либо авторские права, сообщите нам об этом.




Previous Table of Contents Next

The conditional operator is unique because it is trinary (three operands) and because it returns values of all types. It can return a numeric value, a string, a Boolean value, and so on. The first operand is the condition. The condition must be an expression that evaluates to a Boolean value, either true or false. The second operator holds the value that the operator should return if the condition is true. The third operand is the value that the expression evaluates to if the condition is false. The conditional operator is often used with an assignment operator. For example:

var level = (points > 500) ? "Second Level" : "First Level"

The variable level is assigned either "First Level" or "Second Level", depending on the value of the Boolean expression points > 500. If the value of points is greater than 500, the conditional expression evaluates to the string "Second Level", which in turn is assigned to the variable level. If the value of points does not exceed 500, the string "First Level" is assigned to the variable. The first operand (the condition) must be Boolean (a single Boolean value or an expression that evaluates to a single Boolean value). The other operands can be of any type.

Comma Operator

operand1, operand2, operand3, ...

The comma operator is rarely used. You can use it to force the evaluation of a set of expressions. The comma operator is also called a parameter delimiter because it does just that. You probably recall that we used the comma operator in functions when we wanted a function to accept multiple arguments.

var beerNum = 99
document.write(beerNum, " bottles of beer on the wall")

In this example the comma operator delimits the method’s arguments.

Here is another example:

var a = (b = "Hello", alert("Hi"), "Howdy")

The comma operator forces the evaluation of all expressions in the statement. Only the last expression is returned, so the value of a would be "Howdy". This statement is equivalent to the following set of statements:

b = "Hello"
alert("Hi")
var a = "Howdy"

Data Type Operator

typeof operand1

or

typeof (operand1)

JavaScript provides an operator to check the data type of its operand. The operand can be either a literal or a data structure such as a variable, a function, or an object. The operator returns the data type. The expression includes the word typeof followed by the literal or identifier. Here are some examples:

typeof foo == "undefined"// when foo is undefined
typeof eval == "function"   // eval is a built-in function
typeof null == "object"// null is an object
typeof 3.14 == "number"
typeof true == "Boolean"
typeof "a string" == "string"
// all of the expressions are true, of course

The typeof operator is very useful for debugging. Until strong debugging tools are available, you must do all debugging by hand, and detecting the data type of a structure is sometimes essential.

Void Operator

void operand1
void (operand1)

or

javascript:void operand1
javascript:void (operand1)

The void operator, like typeof, is quite extraordinary. It specifies an expression to be evaluated without returning a value. Take a look at the following script:

function foo() {
  alert("Function entered")
  return true
}
alert(foo())

The preceding script segment displays two alert boxes with the following strings:

  • Function entered
  • true

Now take a look at another function and call:

function foo() {
  alert("Function entered")
  return true
}

alert(void foo())

This script also generates two alerts, but the second one reads “undefined,” because the void operator evaluates the function without returning a value. A more important use of this operator comes with hypertext links, where it is used to evaluate a JavaScript expression. The expression is evaluated but is not loaded in place of the current document. The following link does nothing because the expression “0” has no effect in JavaScript:

<A HREF="javascript:void(0)">Click here to do nothing</A>

The following code generates an alert box when the link is clicked:

<A HREF="javascript:void(alert('Wow'))">Click here
	to display message</A>

The parentheses are optional, so it’s up to you to decide on using them. Some scripters specify them in HTML and omit them in JavaScript, with no good reason.

Operator Precedence

You probably remember that 2 + 6 * 9 is 56 and not 72, because multiplication precedes addition. That is exactly the meaning of operator precedence. It is not necessary to remember the precedence rules because parentheses () can be used to force evaluation in the desired order. The expressions are evaluated according to the precedence rules. Operators at the same level are evaluated from left to right. The following table will help you when you want to define complex expressions:

Table 7-14. Operator precedence.


Level Operators Notes

1 () [] . call, member (including typeof and void)
2 ! ~ – ++ – – negation, increment
3 * / % multiply/divide
4 + – addition/subtraction
5 << >> >>> bitwise shift
6 < <= > >= relational
7 == != equality
8 & bitwise AND
9 ^ bitwise XOR
10 | bitwise OR
11 && logical AND
12 || logical OR
13 ?: conditional
14 = += –= *= /= %= <<= >>= >>>= &= ^= |= assignment
15 , comma

Previous Table of Contents Next


With any suggestions or questions please feel free to contact us