# Operators and Operations

Operators in JavaScript are symbols that can perform operations on certain values and data types. The most basic example are the arithmetic operators: `+`

, `-`

, `*`

and `/`

. Operators each have a corresponding operation, can be binary or unary, and can accept a fixed set of values

A binary operator is one that can operate on two values, and a unary operator can operate only on one value

A value that an operator performs an operation on is called an **operand**

The following section has a few tables of operators, their corresponding operations and their accepted operand(s).

## Arithmetic operators

These are the classic addition, subtraction, multiplication and division operators that are nearly standard across languages:

`+`

for addition`-`

for subtraction`*`

for multiplicationand

`/`

for division

These are all binary operators that take two numbers in as operands.

As an aside, the `+`

operator also allows for string concatenation and array concatenation:

### Other math operators

The

`**`

operator allows for exponents:

The

`%`

operator gets the remainder after division

## Comparison operators

These operators allow for, as you can guess, comparing two values. They are binary operators that return a boolean value. The symbols are as follows, with their usage with numbers being as expected:

`a > b`

returns`true`

if`a`

is greater than`b`

`a >= b`

returns`true`

if`a`

is greater than*or equal to*`b`

`a < b`

returns`true`

if`a`

is lesser than`b`

`a <= b`

returns`true`

if`a`

is lesser than*or equal to*`b`

`a !== b`

returns`true`

if`a`

is not equal to`b`

The comparison operators can also be used to compare two strings; they return `true`

or `false`

based on a character-by-character comparison of the two strings. An example is below:

The way the above works is that the first character in each string are compared. If they are equal, then the next character in each string are compared. Eventually, when two characters in a string are unequal, then they are compared according to the operator (after internally converting the characters to their numeric ASCII values) and a boolean value returned accordingly.

If one string ends before the other and all characters up till that point are the same, the longer string is deemed the "greater" value:

### Equality operators

The equality operation has two possible operators, each of which function slightly differently.

The first is the "triple-equals" or the "type-strict equals" operator: `===`

. This operator works as you would expect:

The second is the "double-equals" or the "type-lax equals" operator: `==`

. This operator works similar to the type-strict equals operator, except that it implicity converts the two operands to the same datatype:

It is good practice to stick to more strict typing in your program, to prevent the chance of errors propagating. On the other hand, sometimes it may be prefered to use the type-lax equals, such as in cases where a value has to store the number 2 but it is irrelevant whether it is in a string, array, or as a primitive number. But such cases are rare.

## Logical operators

These operators can take one or two boolean expressions and returns a boolean value. They are often combined with comparison operators.

### AND

The AND operator, `&&`

(double ampersand symbol), is a binary operator that compares two boolean values expressions and returns `true`

**only if both the expressions** evaluate to `true`

. If either one of the expressions evaluate to `false`

, then `&&`

returns `false`

.

### OR

The OR operator, `||`

(double bars), is a binary operator that compares two boolean expressions and returns `true`

if either one of them evaluates to `true`

. If both the expressions evaluate to `false`

, then the operator returns `false`

.

### NOT

The NOT operator, `!`

, is a unary operator that reverses a boolean expression's value. If the expression evaluates to `true`

, it returns `false`

; if the expression evalutes to `false`

then it returns `true`

. It does not change the original expression's value

## Assignment operators

### Value assignment operator

As seen before, this operator has the symbol `=`

and allows you to assign a value to a variable or constant. Example:

### Operation assignment operators

This class of operators are formed by combining a binary logical or mathematical operator with the value assignment operator: `op=`

. They can then be used as an assignment operator, assigning a value to a variable while performing the binary operation on both. Essentially: `a op= b`

is the same as `a = a op b`

. Some examples are below:

### Increment and decrement operators

These two unary operators allow to increment and decrement numeric variable values by 1. The increment operator is a double plus (`++`

) and the decrement operator is a double minus (`--`

). They can be placed either behind or in front of a variable name, and are accordingly called pre- or post-increment or decrement.

The difference between the pre- and post- version of these operators is to do with their return values:

## Bitwise operators

These operators allow you to perform bitwise operations like bitwise AND, OR and NOT on values. To see how they work in detail, visit this site.

## Type conversion

Recall that JavaScript is a weakly-typed language. This means that you can end up with situations like this:

This is one of the reasons it is important to maintain type consistency and stronger typing in your code. It is also wacky issues like this that contributed to the popularity of languages like TypeScript, a strongly-typed version of JavaScript.

## Next steps

Now that we have covered data types and operations concerning these datatypes, the next section will cover a few more coding constructs of JavaScript.

Last updated