Chapter 3: Operators
Assignment Operator
An operator you already know is the assignment operator. Its most basic function is assignment a value to a variable , thereby palcing the value in memory.
For example, the expression x=20 assigns the value 20 to the variable x. When Javascript encounters the assignment operator(=), it first looks to the right for a value. It then looks to the left and ensures that there is a place to store the number.If it finds a variable, it assigns the value to it.In this case, x holds the value of 20. It always works from right to left, so the expression 20=x causes an error in Javascript by trying to assign a new value to 20. This is not allowed , given the fact that 20 is not a variable, but an integer whose value cannot be changed.
Javascript supports 11 other assignment operators that are really a combination of the assignment operator and either an arithmetic or bitwise operator. These shorthand versions follow.
Combination of Assignment and Arithmetic Operators
x+=y is short for x=x+y
x-=y is short for x=x-y
x*=y is short for x=x*y
x/=y is short for x=x/y
x%=y is short for x=x%y
Combination of Assignment and Bitwise Operators
X<<=y is short for x=x<<y
x>>=y is short for x=x>>y
x>>>=y is short for x=x>>>y
x&=y is short for x=x&y
x^=y is short for x=x^y
x|=y is short for x=x|y
Arithmetic Operators
When working with number, you use arithmetic operator.
++i is the same as using i=i+1
--i is the same as using i=i-1
Comparison Operator
Comparison operators are used for just that ,comparing. Expressions that use comparison operators are essentially asking a question about two values. The answer can either be true or false.
Conditional Operators
Javascript uses the set of two operators,? and:, to form conditional expressions return one of two values based on the logical value of another expression. For example , you can use the following conditional expression to alert the user if he is the millionth person to view the page.
resultMsg=(numHits==1000000)?"you have won!" : " You lost.Try again!".
This expression returns the string "you have won!" if numHits is equal to 1000000; otherwise , it returns , "you lost. Try again!" . To put this idea to work , the second line of the previous example displays the result to the user using the built-in alert() function. If numHits is equal to one million, an alert dialog box pops up to let the visitor know.
String Operators
The set of string operators includes the concatenation operator(+), which is also used as the arithmetic operator for addition , and all comparison operator. Using the concatenate operator, you can easily attach strings together to make a larger one.
Boolean Operators
Boolean operators (also called logical operators) are used in conjunction with expressions that return logical values. The following is a list of the three Boolean operators:
&& The logical and operator returns true if both Expression1 and Expression2 is true . Otherwise , it returns false. Note the following examples:
(1>0) && (2>1) returns true.
&&(2<1) returns false.
|| The logical or operator returns true if either Expression1 or Expression2 is true. If neither Expression1 nor Expression2 is true, then it returns false. Note the following examples:
(1>0)||(2<1) returns true.
(1<0)||(2<1) returns false.
! The logical not operator is an unary operator that returns the opposite value of Expression. If Expression is true, it returns false; and if Expression is false, it returns true. This works in the same way as the arithmetic negation operator and will not permanently change the value of Expression. Note the following examples:
!(1>0) returns false.
!(1<0) returns true.
The typeof Operator
The typeof operator returns the type of data that its operand currently holds. This is especially useful for determining if a variable has been defined. Note the following examples:
typeof unescape returns the string "function".
typeof undefinedVariable returns the string "undefined".
typeof 33 returns the string "number" .
typeof "A String" returns the string "String".
typeof true returns the string "boolean".
typeof null returns the string "object".

No comments:
Post a Comment