DEV Community


Posted on

Number in JS

In the name of Allah the most merciful and the most gracious
Today is turn to Number data type in JS which is a primitive data type.
We write number in js without quote unlike string.
For example

let studentGrade=56;
// result->56;
Enter fullscreen mode Exit fullscreen mode

We don’t have any more types for numbers unlike other languages e.g kotlin ,c++ and java.
Just we have number data type.
We can convert number which is inside quote into number.
We can find the type of a data by typing typeof.
Here is the demo:

let number=’4.5’;
console.log(typeof number);
//result -> string
console.log(typeof Number(number));
//result -> number
Enter fullscreen mode Exit fullscreen mode

We can use parseInt() and parseFloat().
The first one converts decimal number into whole number.
The second one converts whole number into decimal number.

let price=45.5;
//result-> 45
Let fruitPrice=10;
//result-> 10.0
Enter fullscreen mode Exit fullscreen mode

Math operations

As we know from school we studied math and it’s operations those operations are in JS like +,-,* and /.
Here we have so many built-in methods to work math here.

let x=9;
let y=5;
let total=x+y;
//result -> 14
let difference=x-y;
let product=x*y;
let division=x/y;
let remainder=x%y;
//result -> 4
Enter fullscreen mode Exit fullscreen mode

The modulo operator returns the remainder.
For example, 4/2=2 and the remainder is zero because these two numbers are fully dividable.
4/3=1.3333333333 and 4%3=1 shows the remaining amount between 4 and 3 is 1.
We can use ** to use power.

let number=2;
let square=number**2;
//result -> 4;
Enter fullscreen mode Exit fullscreen mode

We can use Math.pow() instead of ** which are the same.
Math.max() or Math.min() …

You can learn more about Math built-in operation by clicking the below link

Discussion (0)