Expressions and Operations

Expressions and operators play very important roles in any programming language. To understand this, let's take a simple expression of  

5+7=12

Here in this expression, 5 and 7 are referred to as operands and + is known as the operator while the whole expression is referred to as the operation of addition. JavaScript provides a number of different operators:

  • Arithmetic Operators 
  • Comparison Operators 
  • Logical Operators 
  • Assignment Operators 
Arithmetic Operators 

These are the arithmetic operators that are supported in JavaScript: 

  1. Addition using the symbol + for example 2 + 3 = 5 
  2. Subtraction using the symbol for example 10 - 20 will equals -10
  3. Multiplication using the symbol * for example 10*20 will equals 200 
  4. Division using the symbol / for example 20/10 gives 2 
  5. Modulus using the symbol % which provides the remainder of an integer division for example 20 % 10 will be 0
  6. Increment using the symbol ++ which increases the value of integer by one. For example, 10++ will give 11 
  7. Decrement using the symbol -- which decreases the value of integer by one. For example, 10-- will give 9. 
Comparison Operators 

Comparison operators provide a means to compare two values. The following comparison operators are supported. 

  • Equal operator using the symbol == for comparing two values and giving true result if they are equal to each other. For Example, 10 == 20 is not true (false). 
  • Not equal operator using the symbol != for comparing two values and giving true result if they are not equal to each other. For Example, 10 != 20 is true. 
  • Greater than operator using the symbol > for comparing two values and returning true if the left side is greater than the right side. For Example, 20 > 10 is true. 
  • Less than operator using the symbol < for comparing two values and returning true if the left side is less than the right side. For Example, 10 < 20 is true. 
  • Greater than or equal to operator using the symbol >= for comparing two values and returning true if the left operand is greater than or equal to the right operand. For Example, 20 >= 10 is true. 
  • Less than or equal to operator using the symbol <= forcomparing two values and returning true if the left operand is less than or equal to the right operand. For Example, 20 <= 10 is not true. 
Logical Operators 
  • Logical 'And' using the symbol && returning true if both operands of the expression are true.

For example if x=true and y=true then x && y is true and if x=true and y=false then x && y is false

  • Logical 'Or' using the symbol || returning true if any of the operands is true.

For example if x=true and y=false then x || y is true and if x=false and y=false then x || y is false

  • Logical 'Not' using the symbol ! reverses the logical state of an operand. The logical 'Or' is a unary operator.

For example if x=true then !x is false.

Assignment Operators 
  • Simple Assignment Operator using the symbol =, assigns the value of the right side to the left side.

For example, given y = 5 and z = 10, then x = y+z assigns the value 15 to x

  • Addition and Assignment Operator using the symbol +=, performs the addition of the left operand to the right operand and then assigns the result to the left operand.

For example, given x = 5 and z = 11, then x += z assigns the value 16 to x 

  • Subtraction and Assignment Operator using the symbol -=, performs the subtraction of the right operand from the left operand and then assigns the result to the left operand.  

For example, given x = 11 and z = 5, then x -= z assigns the value 6 to x 

  • Multiplication and Assignment Operator using the symbol *=, performs the multiplication of the right operand with the left operand and then assigns the result to the left operand.

For example, given x = 11 and z = 5, then x *= z assigns the value 55 to x 

  • Division and Assignment Operator using the symbol /=, performs the division of the left operand by the right operand and then assigns the result to the left operand. For example, X/= Z is equal to X=X­/Z 

For example, given x = 10 and z = 5, then x /= z assigns the value 2 to x 

  • Modulus and Assignment Operator using the symbol %=, performs modulus using two operands and assigns the result to the left operand. For example, X%= Z is equal to X=X­%A. 

For example, given x = 11 and z = 5, then x %= z assigns the value 1 to x