JavaScript basics arithmetic operators

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

Doing math in JavaScript is a prevalent task. We often want to perform some sort of calculation on one or two numbers.
This is where arithmetic operators come in! Let's see which ones we can use in JavaScript:
| Operator | Description |
+ | Addition |
++ | Increment |
- | Subtraction |
-- | Decrement |
* | Multiplication |
** | Exponentiation |
/ | Division |
% | Modulus |
Let's have a more detailed view of each of these arithmetic operators in JavaScript.
This can be used to add two numbers, an example:
const a = 5 + 2; // 7
const b = a + 3; // 10
However, be aware the plus sign is also used to combine strings, so doing something like this might surprise you:
const a = '3' + 3; // 33
This happens because it will take the first three as a string and place the number behind it.
This is a super handy operator to quickly increment a number, be aware it has two implementations that can return different results.
In the most basic form it's used like so:
let a = 1;
a++; // 1
console.log(a); // 2
Not that the actual operator does not yet return the new value directly.
We can change that behavior by putting the ++ before it.
let a = 1;
++a; // 2
console.log(a); // 2
However, you rarely want to use the value directly.
As we can do addition, we can also subtract two numbers:
const a = 4 - 2; // 2
Weird enough, this will subtract from a string!
const a = '5' - 2; // 3
Or we can decrement a value.
let a = 5;
a--; // 5
console.log(a); // 4
This is used to multiply two numbers.
const a = 2 * 5; // 10
This is a short-hand way to use the Math.pow() function.
It will raise the first number to the power of the second.
const a = 5 ** 3; // 125
Which is the exact same as:
const a = Math.pow(5, 3); // 125
The division is used to divide two numbers.
const a = 10 / 2; // 5
The modulus operator is also known to get the remainder of a division operation.
const a = 10 % 2; // 0
const b = 10 % 3; // 1
Meaning with the first division, we don't have any number left. For the second one, we will keep the number 1 as a left-over.
And with that, we reached the end of the article. I hope you have a solid understanding of JavaScript arithmetic operators.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter