You are on page 1of 8

Expressions

 
JavaScript allow us to solve algebraic expressions.  operator
 
We have some operators available in JS!

2+3
 
+ addition
- subtraction
* multiplication
/ division operands
** exponentiation
% modulus (division remainder)
Assignment Operators

There is a shortcut assignment in JS when we want to assign to a variable (x) the result
of an operation of it's own value with another value (y).
 
Name Operator Equivalent
Assignment x=y N/A
Addition x += y x=x+y
Substraction x -= y x = x -y
Multiplication x *= y x=x*y
Division x /= y x=x/y
Remainder  x %= y x = x% y
Exponentiation x ** = y x= x**y
Auto-Increment Operators
 
There is also an auto-increment and auto-decrement operator
available in JavaScript. All it does is increase/decrease by one the
variable.
 
x++  // x = x + 1
x-- // x = x - 1
 
It has nothing special to it, it’s just shorter & cleaner!
What are Expressions

An expression is any valid set of literals, variables,


operators, and expressions that evaluates to a single
value.

The value may be a number, a string, or a logical value.


Conceptually, there are two types of expressions:
 
those that assign a value to a variable
those that simply have a value
 
Expressions

Expressions can be more complicated. Let’s look at


 
  ( (7 + 5) / 3) - 8;
Expressions. Operator Precedence

Expressions can be more complicated. Let’s look at


 
8 + 4 / 2 ** 3 - 3;
Can you guess the result?
 
 
Expressions. Operator Precedence

PEMDAS allow us to understand which Precedence Operator Name


operators take precedence in any given 1  ( ) Parentheses
expression in JavaScript. 2  ** Exponents
3  * Multiplication
  3 + 2 ** 3
4  / Division
Following this, we will know that will 5  + Addition
have to evaluate first the exponents to 6  - Substraction
the addition.
 
 
 
Expressions. Operator Precedence
 
var i = 10 + 5 * 2 ** 3 / 4 - 6

// === 10 + 5 * 8 / 4 - 6

// === 10 + 40 / 4 - 6
Precedence Operator
// === 10 + 10 - 6 1 ()
2  **
// === 10 + 4
3  *
// ==> 14 4  /
  5  +
  6  -

You might also like