DEV Community

Cover image for Operators in JavaScript!
Elisa McDonald
Elisa McDonald

Posted on

Operators in JavaScript!

What are Operators?

Operators are special symbols that act on data. They're used to perform operations on values or variables by assigning them to a variable, comparing them, or using them in arithmetic operations.


Arithmetic Operators:

Arithmetic operators are used to perform arithmetic on values (either literal or variables).

+ addition
- subtraction
* multiplication
** exponentiation
/ division
% modulus aka remainder
++ increment
-- decrement

Examples of arithmetic operators being used:

1 + 1 = 2
2 - 1 = 1
2 * 2 = 4
3 ** 2 = 9
4 / 2 = 2
5 % 2 = 1
5++ = 6
5-- = 4
Enter fullscreen mode Exit fullscreen mode

Assignment Operators:

Assignment operators assign a value to a variable.

Examples of assignment operators being used:

x = 2
x += 2 // reassigns x to whatever it was + 2
x -= 2 // reassigns x to whatever it was - 2
x *= 2
x /= 2
x %= 2
x **= 2
Enter fullscreen mode Exit fullscreen mode

Comparison Operators:

Comparison operators compare two values to determine equality or difference and evaluate to a boolean aka true or false.

== loosely equal to
=== strictly equal to

!= loosely not equal to
!== strictly not equal to

The difference between loosely equal / not equal & strictly equal to:
Loosely checks if just value are equal.
Strictly checks if both the value and data type are equal.

Helpful Tip: Don't use non-strict comparison unless there's a good reason to which is almost never!

> greater than
< less than
>= greater than or equal to
<= less than or equal to

Examples of comparison operators being used:

x = 5
y = 10

x == 5 // returns true
x == '5' // returns true

x === 5 // returns true
x === '5' // returns false

y != 10 // returns false
y != '10' // returns false

y !== 10 // returns false
y !== '10' // returns true

x > 10 // returns false
x < 10 // returns true

y >= 10 // returns true
y <= 10 // returns true
Enter fullscreen mode Exit fullscreen mode

Logical Operators:

Logical operators compare two variables or values and determine the logic whether it's true or false

&& and
|| or
! not

Examples of logical operators being used:

x = 2 
y = 6

// if all inequalities are true it returns true
x < y && y > 8

// if at least one of the inequalities are true it returns true
x > y || y < 4

// logical opposite
!x === 3
Enter fullscreen mode Exit fullscreen mode

Ternary Operators:

The only operator that takes 3 operands: a condition followed by a ? then an expression to execute if the condition is truthy followed by a : and then the expression to execute if the condition is falsy. The ternary operator is an alternative to using an if else statement.

let number = 99;

let lessThan100 = number < 100 ? "Less than 100!" : "Sorry your number is not under 100."

console.log(lessThan100); // "Less than 100!"
Enter fullscreen mode Exit fullscreen mode

Conclusion:

Operators are important tools in JavaScript for performing various operations on data. Each type of operator serves a specific purpose. Understanding how to use operators can help write more efficient and readable code.

Happy coding! :)

Top comments (0)