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
Assignment OperatorsAnd many more. In this post, let's look at some of these operators.
Mathematical or Arithmetic Operators
We already use the
- operators, but of course, we can do all Arithmetic Operations. We can also do
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 is not only a Mathematical Operator but also can be used with strings. When used with strings, it joins the
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.
The most straightforward
assignment operator is just the equal sign
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.
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.