# JavaScript Operators

= is used to assign values, + is used to add values, ++ is used to increment values.

The assignment operator **=** is used to assign values to JavaScript variables.

The arithmetic operator + is used to add values together.

## Example

Assign values to variables and add them together:

z = 2; // assign the value 2 to z

x = y + z; // assign the value 7 (y + z) to x

The result of *x* will be:

Try it Yourself »

## JavaScript Arithmetic Operators

Arithmetic operators are used to perform arithmetic between variables and/or values.

Given that **y = 5**, the table below explains the arithmetic operators:

Operator | Description | Example | Result | Result | Try it |
---|---|---|---|---|---|

+ | Addition | x = y + 2 | y = 5 | x = 7 | Try it » |

- | Subtraction | x = y - 2 | y = 5 | x = 3 | Try it » |

* | Multiplication | x = y * 2 | y = 5 | x = 10 | Try it » |

/ | Division | x = y / 2 | y = 5 | x = 2.5 | Try it » |

% | Modulus (division remainder) | x = y % 2 | y = 5 | x = 1 | Try it » |

++ | Increment | x = ++y | y = 6 | x = 6 | Try it » |

x = y++ | y = 6 | x = 5 | Try it » | ||

-- | Decrement | x = --y | y = 4 | x = 4 | Try it » |

x = y-- | y = 4 | x = 5 | Try it » |

## JavaScript Assignment Operators

Assignment operators are used to assign values to JavaScript variables.

Given that **x = 10** and **y = 5**, the table below explains the assignment operators:

Operator | Example | Same As | Result | Try it |
---|---|---|---|---|

= | x = y | x = y | x = 5 | Try it » |

+= | x += y | x = x + y | x = 15 | Try it » |

-= | x -= y | x = x - y | x = 5 | Try it » |

*= | x *= y | x = x * y | x = 50 | Try it » |

/= | x /= y | x = x / y | x = 2 | Try it » |

%= | x %= y | x = x % y | x = 0 | Try it » |

## JavaScript String Operators

The + operator can also be used to concatenate (add) strings.

When used on strings, the + operator is called the concatenation operator. |

## Example

To add two or more string variables together, use the + operator.

txt2 = "nice day";

txt3 = txt1 + txt2;

The result of **txt3** will be:

Try it Yourself »

To add a space between the two strings, insert a space into one of the strings:

## Example

txt2 = "nice day";

txt3 = txt1 + txt2;

The result of **txt3** will be:

Try it Yourself »

or insert a space into the expression:

## Example

txt2 = "nice day";

txt3 = txt1 + " " + txt2;

The result of **txt3** will be:

Try it Yourself »

The += operator can also be used to concatenate strings:

## Example

txt1 += "nice day";

The result of **txt1** will be:

Try it Yourself »

## Adding Strings and Numbers

Adding two numbers, will return the sum, but adding a number and a string will return a string:

## Example

y = "5" + 5;

z= "Hello" + 5;

The result of *x*,* y*, and *z* will be:

55

Hello5

Try it Yourself »

The rule is: **If you add a number and a string, the result will be a string!**

## JavaScript Bitwise Operators

Bit operators work on 32 bits numbers.

Any numeric operand in the operation is converted into a 32 bit number.

The result is converted back to a JavaScript number.

Operator | Description | Example | Same as | Result | Decimal |
---|---|---|---|---|---|

& | AND | x = 5 & 1 | 0101 & 0001 | 0001 | 1 |

| | OR | x = 5 | 1 | 0101 | 0001 | 0101 | 5 |

~ | NOT | x = ~ 5 | ~0101 | 1010 | 10 |

^ | XOR | x = 5 ^ 1 | 0101 ^ 0001 | 0100 | 4 |

<< | Left shift | x = 5 << 1 | 0101 << 1 | 1010 | 10 |

>> | Right shift | x = 5 >> 1 | 0101 >> 1 | 0010 | 2 |

The examples above uses 4 bits unsigned examples. But JavaScript uses 32-bit signed numbers. Because of this, in JavaScript, ~ 5 will not return 10. It will return -6. ~00000000000000000000000000000101 will return 11111111111111111111111111111010 |

## The typeof operator

The **typeof** operator returns the type of a variable (or an
expression):

## Example

var y = "John";

typeof x // Returns number

typeof y // Returns string

typeof 3.14 // Returns number

typeof false // Returns boolean

typeof [1,2,3,4] // Returns object

typeof {name:'john', age:34} // Returns object

Try it yourself »

## The delete Operator

The **delete **operator can be used to delete properties from
objects:

## Example

delete person.age;

The delete operator is designed to be used on object properties. It has no effect on variables or functions.

The delete operator should not be used on predefined JavaScript object properties. It can crash your application.

## The Unary + Operator

The **unary + operator** can be used to
convert a variable to a number:

If the variable cannot be converted, it will still become a number, but with the value NaN (Not a number):

## JavaScript Comparison and Logical Operators

Comparison and logical operators are described in the **JS Comparisons** chapter.

## Your suggestion:

## Thank You For Helping Us!

Your message has been sent to W3Schools.