# JavaScript operators

-

Examples:

``````
var x = 5;         // assign the value 5 to x
var y = 2;         // assign the value 2 to y
var z = x + y;     // assign the value 7 to z (x + y)
``````

The assignment operator (`=`) assigns a value to a variable.

``````
var x = 10;
``````

The addition operator (`+`) adds numbers:

``````
var x = 5;
var y = 2;
var z = x + y; // Result 7
``````

The multiplication operator (`*`) multiplies numbers.

``````
var x = 5;
var y = 2;
var z = x * y; // Result 10
``````

JavaScript Arithmetic Operators

Arithmetic operators are used to perform arithmetic on numbers:

Operator Description
- Subtraction
* Multiplication
** Exponentiation (ES6)
/ Division
% Modulus (Division Remainder)
++ Increment
-- Decrement

Note: Arithmetic operators are fully described in the JS Arithmetic chapter.

JavaScript Assignment Operators

Assignment operators assign values to JavaScript variables.

Operator Example Same As
= x = y x = y
+= x += y x = x + y
-= x -= y x = x - y
*= x *= y x = x * y
/= x /= y x = x / y
%= x %= y x = x % y

The addition assignment operator (`+=`) adds a value to a variable.

``````
var x = 10;
x += 5; // Result 15
``````

Note: Assignment operators are fully described in the JS Assignment chapter.

JavaScript String Operators

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

``````
var txt1 = "John";
var txt2 = "Doe";
var txt3 = txt1 + " " + txt2;      // Result "John Doe"
``````

The `+=` assignment operator can also be used to add (concatenate) strings:

``````
var txt1 = "What a very ";
txt1 += "nice day";      // Result "What a very nice day"
``````

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

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

``````
var x = 5 + 5;           // Result 10
var y = "5" + 5;         // Result 55
var z = "Hello" + 5;     // Result Hello5
``````

Remember: If you add a number and a string, the result will be a string!

JavaScript Comparison Operators

Operator Description
== equal to
=== equal value and equal type
!= not equal
!== not equal value or not equal type
> greater than
< less than
>= greater than or equal to
<= less than or equal to
? ternary operator

Note: Comparison operators are fully described in the JS Comparisons chapter.

JavaScript Logical Operators

Operator Description
&& logical and
|| logical or
! logical not

Note: Logical operators are fully described in the JS Comparisons chapter.

JavaScript Type Operators

Operator Description
typeof Returns the type of a variable
instanceof Returns true if an object is an instance of an object type

Note: Type operators are fully described in the JS Type Conversion chapter.

Exercise 1

Multiply `10` with `5`, and alert the result:

``````
``````

Exercise 2

Divide `10` by `2`, and alert the result:

``````
``````

Exercise 3

`x = 10`

`y = 5`

Use the correct assignment operator that will result in `x` being `15` (same as `x = x + y` ).

``````
x = 10;
y = 5;
x  += y;
``````

Exercise 4

`x = 10`

`y = 5`

Use the correct assignment operator that will result in `x` being `50` (same as `x = x * y` ).

``````
x = 10;
y = 5;
x *= y;
``````