Data Types in JavaScript : Number

Number is the numeric data type. Number can be in form of integer, float, double etc. Let’s see how to work with numbers in JavaScript & perform some operations.

Before starting with numbers let’s look at some common arithmetic operators which we can use with numbers.

Addition (+), Subtraction (-), Multiplication (*), Division (/), and Modular (%) these are most common arithmetic operators we use to perform mathematical operations on numbers.

Let’s use these operators with numbers.

Addition (+)

For addition of numbers we use plus (+) sign in JavaScript. Let’s see addition example.

‎
let result;
let a = 20;
let b = 30;
result = a + b;
console.log(result);
‎

We have created 3 variables. One to store results and others (a & b) are holding values. By using (+) sign we are adding those variable’s values and storing into result variable.

In the console log, you should see result is 50.

Subtraction (-)

For subtraction of numbers we use minus () sign in JavaScript. Let’s see subtraction example.

‎
let result;
let a = 30;
let b = 10;
result = a - b;
console.log(result);
‎

For Subtraction, we have used minus () sign. In the console log you will get an answer 20.

Multiplication (*)

Normally for multiplication we use X sign. But in programming languages we use asterisk * for multiplication. Let’s see example.

‎
let result;
let a = 3;
let b = 5;
result = a * b;
console.log(result);
‎

For multiplication, we have used * sign. You will get an answer 15 in console log.

Division (/)

For division of numbers we use forward slash (/) operator in JavaScript. Let’s see the example.

‎
let result;
let a = 10;
let b = 5;
result = a / b;
console.log(result);
‎

For division, we have used / sign. You will get an answer 2 in the console log.

Modulo (%)

For modulo we use percentage (%) sign. It basically returns remainder after division. Let’s see the example.

‎
let result;
let a = 7;
let b = 3;
result = a % b;
console.log(result);
‎

We have used % sign between a and b variables. You will get an answer 1 in the console log.

Increment (++) & Decrement (- -) Operators

When we use ++ operator as postfix (means after the number or numeric variable) it returns value first and then increment and then returns incremented value.

When we use ++ operator as prefix (means before the number or numeric variable) it increments first and returns value.

If explanation seems confusing 😕 let’s see the example.

Increment (++) Example

‎
let a = 22;
console.log('a1 =' , a++);
console.log('a2 =' , a);

let b = 22;
console.log('b1 =' , ++b);
console.log('b2 =' , b);
‎

We have declared two variables (a & b) with same value.

For a variable, we are logging a++ (postfix) value & on the next line we are logging a value. Results are:

a1 = 22 & a2 = 23

You can see that even if we have used ++ after variable it doesn’t increment value at a1 but at a2 it has increment value. So, now read the above statement for postfix you will get more clarity now.

For b variable, we are logging ++b (prefix) value & on the next line we are logging b value. Results are:

b1 = 23 & b2 = 23

Here for prefix, value gets incremented first. Thus, value for both b1 and b2 logs are same. Read the above statement for prefix you will get more clarity now.

Decrement (- -) Example

Decrement is almost same as increment. It is just that in decrement value gets subtracted instead of addition. Let’s see the example.

‎
let a = 22;
console.log('a1 =' , a--);
console.log('a2 =' , a);

let b = 22;
console.log('b1 =' , --b);
console.log('b2 =' , b);
‎

You can see that we have used – – sign with variables a and b.

Results for variable a:

a1 = 22 & a2 = 21

Results for variable b:

b1 = 21 & b2 = 21

Negative Numbers

You can also define negative numbers same way as postive numbers. For exmaple.

‎
let amount = -50;
console.log(amount);
‎

You will get -50 in console.

If you want to add or minus negative number then make habit using brackets. For example:

‎
let result = 22 + (-7);
console.log(result);
‎

Answer of above is 15. You can see in console. By using brackets you make sure that your mathematical operation will as expected.

That’s it. This is all about numbers in JavaScript. You can start experiment with it using other operators.😊

Categories js

Subscribe Now!

Subscribe Us For Latest Articles