Hello, curious stranger on the internet, today we are going to learn about handling numerical values in Javascript.
In the previous part of fundamentals of Javascript, we covered why let
is preferred over var
in Javascript.
Let's continue our journey to deep dive into the Javascript world from that point.
Declaring integer values
We can declare integer values in JS by using let
or const
keyword as shown below:-
const radius = 5;
console.log("The radius of circle is",radius);
const diameter = 2 * radius;
console.log("The diameter of circle is", diameter);
/*
Output:-
The radius of circle is 5
The diameter of circle is 10
*/
We can also calculate the area of a circle in the above example by using the Math.pow function:-
const pi = 3.14;
area = pi * Math.pow(radius, 2);
console.log("The area of circle is", area);
/*
Output:-
The area of circle is 78.5
*/
Negative integers can be declared in a similar way:-
let negativeInteger = -1;
console.log("Value of Integer is", negativeInteger);
/*
Output:-
Value of Integer is -1
*/
NaN in Javascript
In javascript, NaN property represents "Not-a-Number" value. This property indicates that a value is not a legal number. Example for the same could be as shown below using Math.sqrt method:-
let negativeInteger = -1;
console.log("Value of Integer is", negativeInteger);
const squareRootOfNegativeInteger = Math.sqrt(negativeInteger);
console.log("Square root of negative integer is", squareRootOfNegativeInteger);
/*
Output:-
Value of Integer is -1
Square root of negative integer is NaN
*/
In the above example, since we tried to calculate square root of a negative integer, the value returned by JS interpreter was NaN.
Cube root
We can calculate cube root of a number using Math.cbrt
method:-
const cubeValue = 125;
const cubeRootValue = Math.cbrt(cubeValue);
console.log("Cube root of",cubeValue,"is", cubeRootValue);
/*
Output:-
Cube root of 125 is 5
*/
Decimal values
Decimal values can be stored using let
and const
keywords similar to integers:-
const base = 6.3;
const height = 3.9;
const area = (base * height) / 2;
console.log("Area of triangle is",area);
/*
Output:-
Area of triangle is 12.285
*/
Infinity in Javascript
In javascript, Infinity
is a numeric value that represents positive infinity.
Example:-
const integerValue = 3;
const divideByZero = 3/0;
console.log("Value after dividing by Zero", divideByZero);
/*
Output:-
Value after dividing by Zero Infinity
*/
Arithmetic Operations
We can perform addition operation in Javascript using +
sign:-
const firstNumber = 7;
const secondNumber = 8;
const addition = firstNumber + secondNumber;
console.log("The sum of two numbers is",addition);
/*
Output:-
The sum of two numbers is 15
*/
By mistake, if we add a String with an Integer, Javascript will concatenate the numbers instead of adding them as shown below:-
const firstNumber = "10";
const secondNumber = 5;
const addition = firstNumber + secondNumber;
console.log("addition of the two numbers is",addition);
/*
Output:-
addition of the two numbers is 105
*/
We can overcome this problem by using parseInt method in Javascript:-
const firstNumber = "10";
const secondNumber = 5;
const addition = parseInt(firstNumber) + secondNumber;
console.log("addition of the two numbers is",addition);
/*
Output:-
addition of the two numbers is 15
*/
This is usually required when data received from the server end is in string
format and we need to convert it to number
format before using it in our frontend code.
Other arithmetic operations
Subtraction, multiplication and division is carried out using -
, *
and /
sign respectively:-
const firstNumber = 3;
const secondNumber = 14;
const difference = firstNumber - secondNumber;
console.log("difference between two numbers is",difference);
const multiplication = firstNumber * secondNumber;
console.log("multiplication of the two numbers is",multiplication);
const division = firstNumber / secondNumber;
console.log("division of the two numbers is",division);
/*
Output:
difference between two numbers is -11
multiplication of the two numbers is 42
division of the two numbers is 0.21428571428571427
*/
We will continue learning more about Javascript language in the upcoming articles in the series. I hope you found the article useful.
Which is your favourite feature of Javascript language?
Do share it in the comments below.
I create content about building web applications and general programming. If this is something that interests you, please share the article with your friends and connections. You can also subscribe to my newsletter to get updates every time I write something!
Thank you for reading, If you have reached so far, please like the article, It will encourage me to write more such articles. Do share your valuable suggestions, I appreciate your honest feedback!
I would love to connect with you on Twitter | Instagram.
You should definitely check out my other Blogs:
See you in my next article, Take care!!