*Last modified: July 11, 2017*

JavaScript operators are used in expressions to evaluate different operands. Expressions are statements that return values.

Here is an example of a simple JavaScript expression:

`2 + 2`

//returns 4

In this expression, we use the arithmetic operator + to add two operands (2 and 2). This expression returns 4.
Operators can be classified as arithmetic, relational, logical, bitwise, or assignment. Additionally, the ternary operator exists for evaluating conditional statements. Following is an explanation of each operator in more detail:

These operators perform all arithmetic operations in JavaScript, including:

`2 + 2`

//returns 4

`4 - 2`

//returns 2

`5 * 5`

//returns 25

`4 / 2`

//returns 2

`6 % 5`

//returns 1

`2++`

//returns 3

`2--`

//returns 1

These operators compare different values and return either true or false based on the expression. They include:

`2 > 1`

//returns true

`2 < 1`

//returns false

`2 >= 2`

//returns true

`1 <= 2`

//returns true

`6 == 5`

//returns false

`2 != 3`

//returns true

Logical operators are used to combine conditions. They include:

`true && false`

//returns false because only one condition is true

`true || false`

//returns true because at least one condition is true

`!true`

//returns false

These operators are used to change the value assigned to a given variable. These variables provide " short cuts" for assigning variables.

`x = 2`

// assigns the value 2 to x

`var x = 2`

x += 1

// x now equals 3

`var x = 2`

x -= 1

// x now equals 1

`var x = 2`

x *= 2

// x now equals 4

`var x = 2`

x /= 2

// x now equals 1

Bitwise operators perform operations on the binary representation of their arguments. These operators are less commonly used and only relevant for hyper-performance programs and apps. They include:

`a & b`

// returns a 1 in each bit position if bits of both a and b are 1

`a & b`

// returns a 1 in each bit position if bits of both a and b are 1

`a | b`

// returns a 1 in each bit position if bit of either a or b is 1

`a ^ b`

// returns a 1 for each bit where one (but not the other) bit is 1

`~ a`

// flips all the bits in a so 0 becomes 1 and 1 becomes 0

`a << b`

// shifts a in binary representation b bits to the left, shifting in zeros from the right

`a >> b`

// shifts a in binary representation b bits to the right, discarding bits shifted off

`a >>> b`

// shifts a in binary representation b bits to the right, discarding bits shifted off, and shifting in zeros from the left.

If bitwise operators confuse you, that's ok. These are seldomly used in the real world and have very specific use cases. It should be noted that these operations still return a numeric (non-binary) value.

This miscellaneous operator provides some syntactic sugar for writing an if..else statement in JavaScript.

`true ? 2 : 3`

// returns 2

This operator takes some getting used to, but can save valuable lines of code as an alternative to if..else statements. The example reads "if true, return 2, otherwise return 3".

This is another miscellaneous operator that returns the type of a given value. For example:

`typeof 3`

// returns 'number'

Since = is reserved for assigning variables, we use == to compare equality. Furthermore, the == operator will implicitly convert types before checking for equality. To check equality without converting types, we use ===. For example:

`0 == false //returns true`

0 === false //returns false

The first statement returns true because the == operator implicitly converts false to it's numeric value 0 before comparing the two values. By using === we ignore this implicit conversion, resulting in false.

Operators are used in expressions to evaluate operands and are the basic building blocks of JavaScript logic. While arithmetic operators perform math based operation, assignment operators change the value of variables. Logical operators evaluate combined conditions. Bitwise operators are seldomly used but worth exploring.

Next, we'll look at decision making in JavaScript.

Write a response...

Submit

Node.js Performance Tips
The V8 JavaScript Engine
Optimize JavaScript for the V8 Engine
JavaScript Performance Tips
Introduction to JavaScript Linters
Vue vs React
An Introduction to Browserify
4 Easy Steps to Using Webpack with Your Node.js App
The Ultimate Guide to JavaScript Build Tools
Top 4 Reasons to Use Angular