Operators

Binary vs. Unary Operators:

  • Binary: operand operator operand, Ex. x + y, x = 1
  • Unary:
    • operator operand: ! isOK, ++i
    • operand operator: i++

Operators:

Assignment =, +=, -=, *=, %=, /=, ...
Comparison <, >, <=, >=, ==, !=
Arithmetic +, -, ++, --, *, /, %
Bitwise ~, <<, >>, >>>, &, |, ^
Logical &&, ||, !
String +, +=
Special operators ? :, delete, new, typeof, void

Assignment operators

Comparison operators

Operator Description Examples returning true, if var1 equals 3 and var2 equals 4.
Equal (==) Returns true if the operands are equal.
3 == var1  
Not equal (!=) Returns true if the operands are not equal.
var1 != 4  
Greater than (>) Returns true if left operand is greater than right operand.
var2 > var1  
Greater than or equal (>=) Returns true if left operand is greater than or equal to right operand.
var2 >= var1
var1 >= 3  
Less than (<) Returns true if left operand is less than right operand.
var1 < var2  
Less than or equal (<=) Returns true if left operand is less than or equal to right operand.
var1 <= var2
var2 <= 5

Arithmetic Operators

  • +, -, *, /
  • %, ++,--,-
Operator Description Example
%
(Modulus)
Binary operator. Returns the integer remainder of dividing the 2 operands. 12 % 5 returns 2.
++
(Increment)
Unary operator. Adds one to its operand. If used as a prefix operator (++x), returns the value of its operand after adding one; if used as a postfix operator (x++), returns the value of its operand before adding one. If x is 3, then ++x sets x to 4 and returns 4, whereas x++ sets x to 4 and returns 3.
--
(Decrement)
Unary operator. Subtracts one to its operand. The return value is analogous to that for the increment operator. If x is 3, then --x sets x to 2 and returns 2, whereas x++ sets x to 2 and returns 3.
-
(Unary negation)
Unary operator. Returns the negation of its operand. If x is 3, then -x returns -3.

Bitwise operators

Operator Usage Description
Bitwise AND
a & b  
Returns a one in each bit position if bits of both operands are ones.
Bitwise OR
a | b  
Returns a one in a bit if bits of either operand is one.
Bitwise XOR
a ^ b  
Returns a one in a bit position if bits of one but not both operands are one.
Bitwise NOT
~ a  
Flips the bits of its operand.
Left shift
a << b  
Shifts a in binary representation b bits to left, shifting in zeros from the right.
Sign-propagating right shift
a >> b  
Shifts a in binary representation b bits to right, discarding bits shifted off.
Zero-fill right shift
a >>> b  
Shifts a in binary representation b bits to the right, discarding bits shifted off, and shifting in zeros from the left.

Logical operators

Operator Usage Description Example1
and (&&)
expr1 && expr2  
Returns expr1 if it converts to false. Otherwise, returns expr2. var1 && var2 returns "Dog".
var2 && var3 returns false
or (||)
expr1 || expr2  
Returns expr1 if it converts to true. Otherwise, returns expr2. var1 || var2 returns "Cat".
var3 || var1 returns "Cat".
var3 || (3==4) returns false.
not (!)
!expr  
If expr is true, returns false; if expr is false, returns true. !var1 returns false.
!var3 returns true.
1 Assume var1 is "Cat", var2 is "Dog", and var3 is false.

String

Example 1:

mStr = "My Lovely ";
hStr = "Home";
oStr = "Office";
mhStr = mStr + hStr; // mhStr is "My Lovely Home".
mStr += oStr; // mStr is "My Lovely Offic".

Example 2:

aNum = 19;
aStr = "My seat No. is ";
aStr += aNum; //The value of aNum is automatically converted to "19".
// Now aStr is "My seat No. is 19".

Special Operators

(condition) ? val1 : val2
var1 = 3;
var2 = (var1 >= 0) ? "Positive" : "Negative";
// Now var2 is "Positive".
delete
Deletes a property from an object, or removes an element from an array.
delete obj1.propertX;
delete arr[6];
new
Create an instance of an object.
The type of the object can be of user-defined or one of the object types: Array, Boolean, Date, Function, Image, Number, Object, Option, RegExp, or String.
Example 1:

function area() {
  return this.radius*this.radius*3.14159;
}
function myCircle(x,y,r) {
  this.x_axis=x;
  this.y_axis=y;
  this.radius=r;
  this.area=area;
}
cir = new myCircle(100,100,25);
ar1 = cir.area();

Example 2:

img1=new Image();
img1.src="icon1.gif";
typeof
Returns a string indicating the type of the operand.
Possible returned string: "object", "number", "string", "boolean", "function", "undefined"
var today=new Date();
typeStr = typeof today; // or typeof(today)
// Now typeStr is "object".
void
The void operator is used in either of the following ways:
1. javascript:void (expression)
2. javascript:void expression

The void operator specifies an expression to be evaluated without returning a value.