Practice
Resources
Contests
Online IDE
New
Free Mock
Events New Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
logo
Events
Attend free live masterclass hosted by top tech professionals
New
Scaler
Explore Offerings by SCALER

Math methods in JS

In JavaScript, we have math methods that can be used without actually creating them. It allows us to perform mathematical tasks on numbers.

Example: Math.LN2
It returns the natural logarithm of 2.

Some of the math properties(constants) are:

 Math.E        // returns Euler's number
Math.PI // returns PI
Math.SQRT2 // returns the square root of 2
Math.SQRT1_2 // returns the square root of 1/2
Math.LN2 // returns the natural logarithm of 2
Math.LN10 // returns the natural logarithm of 10
Math.LOG2E // returns base 2 logarithm of E
Math.LOG10E // returns base 10 logarithm of E

Some of the Math methods are:

abs(x)        // Returns the absolute value of x
cbrt(x) // Returns the cubic root of x
ceil(x) // Returns x, rounded upwards to the nearest integer
clz32(x) // Returns the number of leading zeros in a 32-bit binary representation of x
exp(x) // Returns the value of Ex
expm1(x) // Returns the value of Ex minus 1
floor(x) // Returns x, rounded downwards to the nearest integer
fround(x) // Returns the nearest (32-bit single precision) float representation of a number
log(x) // Returns the natural logarithmof x
log10(x) // Returns the base-10 logarithm of x
log1p(x) // Returns the natural logarithm of 1 + x
log2(x) // Returns the base-2 logarithm of x
pow(x, y) // Returns the value of x to the power of y
random() // Returns a random number between 0 and 1
round(x) // Rounds x to the nearest integer
sign(x) // Returns the sign of a number (checks whether it is positive, negative or zero)
sqrt(x) // Returns the square root of x
trunc(x) // Returns the integer part of a number (x)
max(x, y, z, ..., n) // Returns the number with the highest value
min(x, y, z, ..., n) // Returns the number with the lowest value

Example: Suppose we want to find the max among 1,7 and 77, then Math.max(1,7,77) will give us the required results.

Some of the Trigonometric methods are:

acos(x)       // Returns the arccosine of x, in radians
acosh(x) // Returns the hyperbolic arccosine of x
asin(x) // Returns the arcsine of x, in radians
asinh(x) // Returns the hyperbolic arcsine of x
atan(x) // Returns the arctangent of x as a numeric value between -PI/2 and PI/2 radians
atan2(y, x) // Returns the arctangent of the quotient of its arguments
atanh(x) // Returns the hyperbolic arctangent of x
cos(x) // Returns the cosine of x (x is in radians)
cosh(x) // Returns the hyperbolic cosine of x
sin(x) // Returns the sine of x (x is in radians)
sinh(x) // Returns the hyperbolic sine of x
tan(x) // Returns the tangent of an angle
tanh(x) // Returns the hyperbolic tangent of a number

Try the following example in the editor below.

You are given an integer N, you have to print the nearest integral value for the base-2 logarithm of N.

Sample Input:

101

Sample Output:

7

Start solving Math methods in JS on Interview Code Editor
Hints
  • Hints are not available for this problem

Discussion


Loading...
Click here to start solving coding interview questions
Free Mock Assessment
Fill up the details for personalised experience.
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Graduation Year *
Graduation Year *
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
*Enter the expected year of graduation if you're student
Current Employer
Company Name
College you graduated from
College/University Name
Job Title
Job Title
Engineering Leadership
Software Development Engineer (Backend)
Software Development Engineer (Frontend)
Software Development Engineer (Full Stack)
Data Scientist
Android Engineer
iOS Engineer
Devops Engineer
Support Engineer
Research Engineer
Engineering Intern
QA Engineer
Co-founder
SDET
Product Manager
Product Designer
Backend Architect
Program Manager
Release Engineer
Security Leadership
Database Administrator
Data Analyst
Data Engineer
Non Coder
Other
Please verify your phone number
Edit
Resend OTP
By clicking on Start Test, I agree to be contacted by Scaler in the future.
Already have an account? Log in
Free Mock Assessment
Instructions from Interviewbit
Start Test