Javascript is the most popular and trending technology in the tech world, Javascript has been started as a simple programming language that intends to add a few interactive features to the webpage and now it's become a very powerful programming language that’s used in every website javascript is used in giant web applications like Uber, Facebook, Google maps Etc.., In this article, we will learn about javascript and Basics of Number methods used in Javascript.
Types of Number methods
Math.random() :
Javascript provides a method random called Math. random() returns a random number between 0 with an Inclusive and 1 with an exclusive
Syntax:
Math. random();
Example:
\> Math.random()
0.6260016299641209
With the generated random numbers, we can generate operations like multiplications, the sum of the numbers, an integer between two values Etc...
Example :
Math.random() * 100
29.597296768756685
Math.round(x) :
Javascript Math. round() method returns the value of x rounded to its nearest integer. When it comes to the fractional part that number is greater than or equal to .5, the argument is rounded to the next highest integer. If the fractional part of the number is less than .5, the argument is rounded to the next lowest integer and we can also implement a random method in the round to get the round value of a random number, Initialise a value in var, By using Math. round() method round the input given is rounded, The next process is to round the valve we got in the random method.
Syntax:
Math.round();
Example:
var n = 12.7
undefined
Math.round(n)
13
Math.round(Math.random() \100)*
19
typeof (x) :
In javascript on a webpage, any input given by a user will always be a string, To find what type of input we give in the code we used a method called a typeof which returns what type of variables we used, In the given an example below we have taken n1 and n2 lets assign n1 as 10 and n2 as a string of ‘20’, By using the type of method we will get an Output of n1 as number and n2 as String, To convert a string to the number we use ParseInt (n2) after compilation the string is converted to a number.
Code:
var n1 = 10
undefined
var n2 = '20'
undefined
typeof n1
'number'
typeof n2
'string'
Math.PI :
The Math.PI is a property in JavaScript that is simply used to find the value of Pi in symbolic form Π which is nothing but it is the ratio of the circumference of a circle to its diameter, whose value is approximately 3.14.
Syntax:
Math.PI
OUTPUT :
3.141592653589793
Math.abs() :
Math. abs() method is mainly used to return the absolute value of a number. Math.abs() uses a number as its parameter and returns its absolute value.
Syntax:
Math.abs(value)
EXAMPLE:
values = Math.abs(67);
console.log(values); // 67
value = Math.abs(-7);
console.log(value); // 7
Math ceil() :
Math ceil() method increases the value of the given input number up to the closest integer value and returns the Output value.
Syntax:
Math.ceil(num)
EXAMPLE:
console. log(Math. ceil(.97)); // Expected output: 1.
Conclusion:
As we discussed the above-mentioned are some of the most important JavaScript Number Methods You Should Know in 2023.