The JavaScript Diaries: Part 14

By Lee Underwood

WEBINAR: On-demand webcast

How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >


Name Description
abs() Returns the absolute value of a number
acos() Returns the arccosine (in radians) of a number
asin() Returns the arcsine (in radians) of a number
atan() Returns the arctangent (in radians) of a number
atan2() Returns the arctangent of the quotient of its arguments
ceil() Returns the smallest integer greater than or equal to a number
cos() Returns the cosine of a number
exp() Returns Enumber, where number is the argument, and E is Euler's constant, the base of the natural logarithms
floor() Returns the largest integer less than or equal to a number
log() Returns the natural logarithm (base E) of a number
max() Returns the greater of two numbers
min() Returns the lesser of two numbers
pow() Returns base to the exponent power, that is, base exponent
random() Returns a pseudo-random number between 0 and 1
round() Returns the value of a number rounded to the nearest integer
sin() Returns the sine of a number
sqrt() Returns the square root of a number
tan() Returns the tangent of a number

Usage in Scripts

Let's see what we can do with some of these methods. (By now you can probably figure out these scripts without an explanation, so I won't go into greater detail. Instead, we'll focus on the methods themselves. Still, if a script begins to become complex, I'll stop and explain it. If you need further help, you can always e-mail me.)

Finding the Square Root

An obvious use is to find the square root of a number. Here's a basic script for doing the calculation:

This then would be called by the following, placed in the body of the document:

Rounding Numbers

Another calculation we could perform would be to round off a number. This script uses the code we wrote previously to find the circumfrence of a circle.

You could also use the round() method to set the number of decimal places:

The formula for rounding a number to x decimal points is:

  1. multiply the original number by 10^x (10 to the power of x)
  2. apply Math.round() to the result
  3. divide result by 10^x

Keep in mind that the format is as follows:

  1. Math.round(10*x)/10; (rounds to tenths)
  2. Math.round(100*x)/100; (rounds to hundredths)
  3. Math.round(1000*x)/1000; (round to thousandths)

Page 2 of 3

Previous Page
1 2 3
Next Page

Make a Comment

Loading Comments...

  • Web Development Newsletter Signup

    Invalid email
    You have successfuly registered to our newsletter.
Thanks for your registration, follow us on our social networks to keep up-to-date