# Javascript Operators

JavaScript has the following types of operators:

• Arithmetic Operators
• String Operators
• Assignment Operators
• Comparison Operators
• Bitwise Operators
• Logical Operators
• Special Operators

### Arithmetic Operators

Following arithematic operators supported by JavaScript.

Assume variable A holds 5 and variable B holds 10 then:

OperatorDescriptionExample
+This will add two operands A + B will give 15
-This will subtract second operand from the first A - B will give -5
*This will multiply both the operands A * B will give 50
/This will divide numerator by denominator B / A will give 2
%This is modulus operator and remainder of after an integer division B % A will give 0
++This will increase integer value by one A++ will give 6
--This will decrease integer value by one A-- will give 4

### String Operators

In addition to the comparison operators, which can be used on string values, the concatenation operator (+) concatenates two string values together, returning another string that is the union of the two operand strings. For example, "my " + "string" returns the string "my string".

The shorthand assignment operator += can also be used to concatenate strings. For example, if the variable firstName has the value "Charlie", then the expression firstName += " Write" evaluates to "Charlie Write" and assigns this value to firstName.

### Assignment Operators

In Javadcript an assignment operator assigns a value to its left operand based on the value of its right operand. The basic assignment operator is equal (=), which assigns the value of its right operand to its left operand. That is, x = y assigns the value of y to x.

Following assignment operators supported by JavaScript.

Assume variable X holds 5 and variable Y holds 10 then:

OperatorDescriptionExample
=This allows to assign values from right side operands to left side operand Z = X + Y will assigne value of X + Y into Z
+=This allows to add right operand to the left operand and assign the result to left operand. Z += X is equivalent to Z = Z + X
-=This allows to subtract right operand from the left operand and assign the result to left operand. Z -= X is equivalent to Z = Z - X
*=This allows to multiply right operand with the left operand and assign the result to left operand. Z *= X is equivalent to Z = Z * X
/=This allows to divide left operand with the right operand and assign the result to left operand. Z /= X is equivalent to Z = Z / X
%=This allows to take modulus using two operands and assign the result to left operand. Z %= X is equivalent to Z = Z % X

### Comparison Operators

In Javascript a comparison operator compares its operands and returns a logical value based on whether the comparison is true. The operands can be string,numerical, logical, or object values. Strings are compared on lexicographical ordering, using Unicode values. In all cases, if the two operands are not of the same type, JavaScript attempts to convert them to an appropriate type for the comparison.

Following comparison operators supported by JavaScript.

Assume variable A holds 5 and variable B holds 10 then:

OperatorDescriptionExample
==This allows to check if the value of two operands are equal or not. (A == B) is not true.
!=This allows to check if the value of two operands are equal or not. (A != B) is true.
>This allows to check if the value of left operand is greater than the value of right operand. (A > B) is not true.
<This allows to check if the value of left operand is less than the value of right operand. (A < B) is true.
>=This allows to check if the value of left operand is greater than or equal to the value of right operand. (A >= B) is not true.
<=This allows to check if the value of left operand is less than or equal to the value of right operand. (A <= B) is true.

### Bitwise Operators

In Javascript bitwise operators work with 32-bit integers.Bitwise operators perform an operation on the bit (0 or 1).

Following bitwise operators supported by JavaScript.

Assume variable X holds 5 and variable Y holds 10 then:

OperatorDescriptionExample
Bitwise ANDThis is used to performs a Boolean AND operation on each bit of its integer arguments. (X & Y) is 2 .
Bitwise ORThis is used to performs a Boolean OR operation on each bit of its integer arguments. (X | Y) is 3.
Bitwise XORThis is used to performs a Boolean exclusive OR operation on each bit of its integer arguments. Exclusive OR means that either operand one is true or operand two is true, but not both. (X ^ Y) is 1.
Bitwise NOTThis is a unary operator and operates by reversing all bits in the operand. (~Y) is -4 .
Left shiftThis is used to moves all bits in its first operand to the left by the number of places specified in the second operand. New bits are filled with zeros. (X << 1) is 4.
Right shiftThis is used to moves all bits in its first operand to the right by the number of places specified in the second operand. The bits filled in on the left depend on the sign bit of the original operand, in order to preserve the sign of the result. (X >> 1) is 1.
Shift Right with Zero OperatorThis operator is look like the >> operator, except that the bits shifted in on the left are always zero. (X >>> 1) is 1.

### Logical Operators

In Javascript logical operators are typically used with Boolean (logical) values; when they are, they return a Boolean value.

Following logical operators supported by JavaScript.

Assume variable X holds 5 and variable Y holds 10 then:

OperatorDescriptionExample
&&This is logical AND operator. If both the operands are non zero then then condition becomes true. (X && Y) is true.
||This is logical OR Operator. If any of the two operands are non zero then then condition becomes true. (X || Y) is true.
!This is logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. !(X && Y) is false.

### Special Operators

JavaScript provides the following special operators:

• Conditional operator
• Comma operator
• delete
• in
• instanceof
• new
• this
• typeof
• void

Conditional Operator

In JavaScript conditional operator takes three operands. The operator can have one of two values based on a condition.

```<script type="text/javascript">
var isTrue = (age >= 18) ? "adult" : "minor";
</script>
```

Comma Operator

In Javascript comma operator (,) simply evaluates both of its operands and returns the value of the second operand. This operator is used inside a for loop, to allow multiple variables to be updated each time through the loop.

```<script type="text/javascript">
for (var a = 0, b = 10; a <= 10; a++, b--)
document.write("myArray[" + a + "][" + b + "]= " + myArray[a][b]) + "<br/>";
</script>
```

delete

In Javascript delete operator deletes an object, an object's property, or an element at a specified index in an array.

```<script type="text/javascript">
delete objName;
delete objName.property;
delete objName[i];
</script>
```

You can use the delete operator to delete variables declared implicitly but not those declared with the var statement.

in

In Javascript in operator returns true if the specified property is in the specified object.

```<script type="text/javascript">
propNameOrNumber in objName
</script>
```

instanceof

In Javascript instanceof operator returns true if the specified object is of the specified object type.

```<script type="text/javascript">
objectName instanceof objectType
</script>
```

new

In Javascript new operator in used to create an instance of a user-defined object type or predefined object types Array or Boolean or Date or Function or Image or Number or Object or Option or RegExp or String.

```<script type="text/javascript">
var newDate = new Date(2014, 12, 17);
</script>
```

this

In Javascript this keyword is used to refer to the current object. In general, this refers to the calling object in a method.

```<script type="text/javascript">
this.propertyName
<INPUT TYPE="text" NAME="age" SIZE=3 onChange="validate(this, 18, 99);">
</script>
```

typeof

In Javascript typeof operator is used in either of the following ways.

```<script type="text/javascript">
typeof operand
</script>
```
```<script type="text/javascript">
typeof (operand)
</script>
```

Suppose if we define the following variables:

```<script type="text/javascript">
var myFun = new Function("5 + 2");
var shape = "round";
var size = 1;
var today = new Date();
</script>
```

The typeof operator returns the following results for these variables:

```<script type="text/javascript">
var myFun = new Function("5 + 2");
var shape = "round";
var size = 1;
var today = new Date();
</script>
```

void

The void operator specifies an expression to be evaluated without returning a value. In Javascript void operator is used in either of the following ways:

```<script type="text/javascript">
void expression;
void (expression);
</script>
```

 Javascript Built in String Object Javascript Built in Number Object Javascript Built in Math Object Javascript Built in Date Object Javascript Built in Arrays Object Javascript Built in Boolean Object Javascript Built in RegExp Object Javascript Built in JSON Object

JAVASCRIPT FEATURED
JAVASCRIPT BUILT-IN OBJECTS
JAVASCRIPT OBJECT