# Javascript Operators

## Use Javascript operators to manipulate Data Objects in a number of ways.

## Javascript Operators

Javascript specifies a set of operators that can be used to manipulate data values.

Javascript operators are used for arithmetic expressions, logical expressions, comparison expressions , assignment expressions and more.

## Javascript Assignment Operator

Javascript assignment operator " = " is used to evaluate the expression on the right hand side of the equal sign and then assign the result to the variable on the right.

## Example: Javascript - Assignment Operator

Give it a TRY! »## Javascript Arithmetic Operators

Javascript arithmetic operators accept numeric values as operands and then return a single numeric value after performing the operation.

The standard arithmetic operators are addition(+) , subtraction(-) , multiplication(*) , division(/) and modulus(%)

## Example: Javascript - Arithmetic Operators

Give it a TRY! »## Javascript Shortcut Assignment Operator

Javascript supports compound assignment operators as shortcuts, using these you can combine assignment operators with any other operation.

Eg : **a = a + 3** can be written as **a += 3** .

## Table : Assignment Operators

Operators | Example | Description |
---|---|---|

= |
var num = 10; | To assign 10 to the variable num. |

+= |
num += 5 | Add 3 to num and assign result to num |

-= |
num -= 5 | Subtract 5 from num and assign result to num. |

*= |
num *= 5 | Multiply num by 5 and assign result to num. |

/= |
num /= 5 | Divide the num by 5 and assign the result to num. |

**= |
num **= 5 | Square num and assign the result to num. |

%= |
num %= 5 | Divide the num by 5 and assign remiander to num. |

## Example: Javascript Compounded Assignment Operatorss

Give it a TRY! »## Javascript Operators: Autoincrement and Autodecrement

Javascript autoincrement(++) and autodecrement(--) operator is used to auto-increment and auto-decrement the value of operand by 1 respectively.

It is used in two forms, in the first form the operand is **prefixed** with the operator(either ++ or --) and result of which is
addition or subtraction of 1 to/from the operand before the operation is performed

In the second form the operand is **postfixed** with the operator(either ++ or --) and result of which is
addition or subtraction of 1 to/from the operand after the operation is performed

## Table : AutoIncrement and AutoDecrement

Operator | Function | Description | Example |
---|---|---|---|

++x |
Pre-Increment | To add 1 to x before any operation | x=5; ++x; x=6(new) |

--x |
Pre-Decrement | To subtract 1 from x before any operation | x=5; --x; x=4(new) |

x++ |
Post-Increment | To add 1 to x after any operation. | x=5; x++; x=6(new) |

x-- |
Post-Decrement | To subtract 1 from x after any operation | x=5; x--; x=4(new) |

## Example: Javascript Operators- AutoIncrement and AutoDecrement

Give it a TRY! »## Javascript Operators: Concatenation

Javascript concatenation(+) operator is used to join one or more strings.

In case the operands are a mix of strings and numbers,
javascript converts the **numbers to strings**. For Eg: **10 + 8** results in **"108"** and not **"18"**.

For statements involving other operators, Javascript does not convert numeric values to string.

## Table : Javascript Concatenation

Operator | Example | Description |
---|---|---|

+ |
"Mac" + "Book" | Strings get concatenated to form MacBook |

+ |
"40" + 4 | 4 is converted to string and then concatenated, result is 404 |

* or / |
"40" * 4 | Javascript does not convert to string, multiplies them as numericals, result is 160. |

+= |
x="James"; x+="Bond" | Two strings get concatenated and results in the string "JamesBond" |

## Example: Javascript Operator - Concatenation

Give it a TRY! »**Note:**The attribute

**defer**is supported only for external Javascript files and not inline Javascript.

## Javascript Operators: Comparison

In order to compare operands the relational and equailty operators are used, the operands can be a numbers or strings. The result of a comparison operation is Boolean(i.e either True or False).

Two numbers are equal if they have the same numeric value, two strings are equal when they have the same sequence of characters, same length and characters at same positions.

## Table : Javascript Comparison Operators

Operators/Operands | Description |
---|---|

A == B |
A is equal to B |

A != B |
A is not equal to B |

A > B |
A is greater than B. |

A >= B |
A is greater than or equal to B |

A < B |
A is less than B |

A <= B |
A is less than or equal to B |

A === B |
A is identical to B is value and type |

A !== B |
A is not Identical to B |

## Example: Javascript Operators - Comparison

Give it a TRY! »**Note:**Strings are compared letter by letter using uicode values used to represent the numerical value of each letter.

## Javascript Operators: Logical

Javascript logical operators &&, ||, ! are used to perform boolean algebra and are often used in combination with relational expression, creating more complex expressions

The operands involved are evaluated from **left to right**, and results are expression in **Boolean** i.e(true or false).

## Table : Javascript Logical Operators

Operators/Operands | Description |
---|---|

A && B |
True, if both A and B are true; otherwise false |

A || B |
True , if either of A and B is true |

!A |
True, if A is false |

## Example: Javascript Operators - Logical

Give it a TRY! »**Note:**Strings are compared letter by letter using unicode values used to represent the numerical value of each letter.

## Javascript Operators: Conditionals

Javascript Conditional operators are also called as **ternary** operators, as it requires at least three operands.

The result is **Boolean** and is often used as a replacement for **if/else** conditional , the symbol for the operator is
?: (i.e if/else)

## Example: Javascript Operators - Conditionals

Give it a TRY! »**Note:**If the test condition is correct, then the value to the right of

**?**is assigned or else the value after

**:**is assigned.

## Javascript Operators: Bitwise

Javascript Bitwise operators is used to perform low level operation on bits in the binary representation of numbers

Bitwise operands treat their operands as **32 bit** or **64 bit** numbers, and operations are carried on between each corresponding bit of the two operands

## Table : Javascript Bitwise Operators

Operator | Function | Example | Description |
---|---|---|---|

& |
Bitwise AND | A & B | Value 1 is returned in each bit position if both corresponding bits are 1 |

| |
Bitwise OR | A | B | Value 1 is returned in each bit position if one or both corresponding bits are 1 |

^ |
Bitwise XOR. | A ^ B | Value 1 is returned in each bit position if one, but not both, of the corresponding bits are 1 |

~ |
Bitwise NOT | ~A | To Invert the bits of the operands, 1 is converted to 0; 0 converted to 1. |

<< |
Left Shift | A << B | To shift A in binary representation by B bits to the left, shifting in zeros from the right |

>> |
Right Shift | A >> B | To shift A in binary representation by B bits to the right, discarding bits are shifted out |

>>> |
Zero-fill right shift | A>>>B | Shifts A in binary representation by B bits to the right, discarding bits are shifted out, zeros are shifted from the left |

## Example: Javascript Operators - Bitwise

Give it a TRY! »**Note:**Strings are compared letter by letter using uicode values used to represent the numerical value of each letter.