Let's start by defining what an Operator is. An Operator
allows us to transform values or combine multiple values and do all kinds of work with values. And there are many categories of Operators like Mathematical Operators
, Comparison Operators
, Logical Operators
, Assignment Operators
And many more. In this post, let's look at some of these operators.
Mathematical or Arithmetic Operators
We already use the +
and -
operators, but of course, we can do all Arithmetic Operations. We can also do division
, multiplication
, and many more.
const x = 10;
const y = 2;
let result;
// Addition Operator
result = x + y;
console.log(result); // 12
// Substration Operator
result = x - y;
console.log(result); // 8
// Multiplication Operator
result = x * y;
console.log(result); // 200
// Division Operator
result = x/y;
console.log(result); // 5
// Modulous or Reminder Operator
result = x % y;
console.log(result); // 0
// Exponential Operator
result = x ** y // x to the power of y
console.log(result); // 100
Plus Operator
plus
operator is not only a Mathematical Operator but also can be used with strings. When used with strings, it joins the strings
or concatenate
strings.
const firstName = 'Akhil';
const lastName = 'Naidu';
console.log(firstName + lastName); // AkhilNaidu
// We can also join space
console.log(firstName + ' ' + lastName) // Akhil Naidu
plus
operator also concatenate or joins two stringstypeof
We used this operator extensively in one of our previous posts. This is repeated just for the sake of documenting with the rest of the operators. A typeof
operator helps us determine the type of the variable.
const hero = 'Naruto';
console.log(typeof hero) // string
const myAge = 24;
console.log(typeof myAge) // number
const jsIsFun = true;
console.log(typeof jsIsFun) // boolean
let emptyVariable;
console.log(typeof emptyVariable) // undefined
typeof
operatorconst thisIsABug = null;
console.log(typeof thisIsABug) // Object
console.log(typeof null) // Object
undefined
Assignment Operators
The most straightforward assignment operator
is just the equal sign =
.
// x will be assigned 30
// Plus will be executed before the assignment operator
let x = 10 + 20;
console.log(x) // 30
x += 10; // x = x + 10 => 30 + 10
console.log(x) // 40
x *= 2; // x = x * 2 => 40 * 2
console.log(x) // 80
x++; // x = x + 1 => 80 + 1
console.log(x) // 81
x--; // x = x - 1 => 81 - 1
console.log(x) // 80
=
, is actually an operator.Comparison Operators
Comparison Operators
are actually great, we use them to produce boolean
values. It would be better to demonstrate them using a code example similar to assignment operators.
const akhilAge = 25;
const narutoAge = 20;
console.log(akhilAge > narutoAge); // true
console.log(akhilAge < narutoAge); // false
// != means not equal
console.log(akhilAge != narutoAge); // true
comparison operator
should be a boolean
These are very useful when we start making decisions with our code based on conditions like above.
Other comparison operators are >
, <
, >=
, <=
, !=
, !==
, ==
, ===
; introducing some of these comparisons right now is not a good idea, so I kept them for a later post.