Mathematical Constants And Functions In Javascript

Javascript as advanced programming language it supports mathematical constants and functions. In this tutorial we will basically discuss two major parts which are Math properties and Math methods. You will realize that math object is not a constructor like other global objects, therefore methods and properties are static, hence they can be called using math as an object without even creating it. Let’s have syntax used in calling math object.
    
     var pi_val=Math.PI;
     var sine_val=Math.sin(30);
    

1. Math Properties

This marks the first part of our tutorial, here we will be explaining on various math properties adn their return values, which may include; LOG2E, LN2, LN10, E, LOG10E, SQRT1_2, PI, SQRT2. Now we be explaining each and giving the relevant examples;
a) Math-LN2
This property returns natural logarithm of 2 which is approximately 0.693. Here is its syntax;
    
 Math.LN2
    

For better understanding let’s have an example;
<head>
<title>Example for math LN2 math property</title>
</head>
<body>
<script type="text/javascript">
var property_value = Math.LN2
document.write(" Math Property Value is : " + property_value);
</script>
</body>

It will produce;
Math Property Value is: 0.69329938

b) Math-LN10

This property logarithm of 10, which is approximately 2.302. Let’s have a syntax for the same; Math.LN10 for better understanding let’s have an example here;

<head>
<title>Example for math-ln10</title>
</head>
<body>
<script type="text/javascript">
var property_value = Math.LN10
document.write(" Math Property Value is : " + property_value);
</script>
</body>

It will produce;
Math Property Value is: 2.302488273

c) Math-E

It returns base natural logarithms, it is Euler’s constant, whereby is approximately 2.718, the syntax is; Math.E for better understanding let’s have an example here;

<head>
<title>Example for math-e property/title>
</head>
<body>
<script type="text/javascript">
var property_value = Math.E
document.write("Math Property Value is :" + property_value);
</script>
</body>

It will produce;
Math Property Value is: 2.7183112238

d) Math-PI

It returns the ration of the circumference of a circle to its diameter which is 3.14159 approximately. It’s syntax is Math.PI. using the similar examples as given above just replace the property with Math.PI you will obtain 3.1415934235543.

Summary for math properties;

For the remaining math properties we will summarize them as below;

  • Math-SQRT1_2 – It returns the square root of 1/2 which is approximately 0.707.
  • Math-SQRT2 – It returns the square root of 2 which is 1.414 approximately.
  • Math-LOG10E – it returns the base 10 logarithm of E, which is 0.434 approximately.
  • Math-LOG2E – it returns the base 2 logarithm of E which is 1.442 approximately.

NB. For the syntax and examples are similar to the ones explained above, so you may try them yourself.

2. MATH METHODS

This marks second part of our tutorial, which is math methods, as we have discussed in other object, also here we are going to see various math methods which may include; abs(), acos(), asin(), atan(), atan2(), ceil(), cos(), exp(), floor(), log(), max(), min(), pow(), random(), round(), sin(), sqrt(), tan(), toSource(). Now we will be discussing each and give the relevant examples;

a) acos()

It returns the arccosine in radians of a number, the numeric values between -1 and 1 otherwise it returns NaN. its syntax is given by; Math.acos(x); where x is a number, for better understanding let’s have an example here;

<head>
<title>Example for acos math method</title>
</head>
<body>
<script type="text/javascript">
var value = Math.acos(-1);
document.write("First Test Value : " + value );
var value = Math.acos(null);
document.write("<br />Second Test Value : " + value );
var value = Math.acos(30);
document.write("<br />Third Test Value : " + value );
var value = Math.acos("string");
document.write("<br />Fourth Test Value : " + value );
</script>
</body>

It will produce;
First Test Value : 3.141593762873
Second Test Value : 1.57076262887
Third Test Value : NaN
Fourth Test Value : NaN

b) asin()

It returns the arcsine in radians of a number, where the numeric value should be between -pi/2 and pi/2 radians for x between -1 and 1, otherwise it returns NaN. It syntax is given by; Math.asin(x); for better understanding let’s have an example here;

<head>
<title>Example for arcsine method</title>
</head>
<body>
<script type="text/javascript">
var value = Math.asin(-1);
document.write("First Test Value : " + value );
var value = Math.asin(null);
document.write("<br />Second Test Value : " + value );
var value = Math.asin(30);
document.write("<br />Third Test Value : " + value );
var value = Math.asin("string");
document.write("<br />Fourth Test Value : " + value );
</script>
</body>



It will produce;
First Test Value : -1.5707963267948966
Second Test Value : 0
Third Test Value : NaN
Fourth Test Value : NaN

Summary for the math-methods

Here we will look at the other math-methods which will apply the similar examples and have the similar syntax, we will explain their return value when used, to make your experience in learning javascript feel more easier, remember to apply them in the examples above and see the result.

  • abs() – it returns the absolute value of a number
  • ceil() – it returns the the smallest integer greater than or equal to a number
  • cos() – it returns the cosine of a number, with numeric value between -1 and 1 which are cosine of an angle.
  • exp() – it returns Ex, where E is Euler’s constant while x is the argument.
  • floor() – It returns the largest integer less than or equal to a number.
  • log() – It returns the natural logarithm(base E) of a number, but it returns NaN if the value of number is negative.
  • round() – It returns the value of a number, which is rounded to the nearest integer.
  • sin() – It returns the sine of a number, with the values between -1 and 1, representing sine of an angle.
  • sqrt()– It returns the square root of a number, NaN is returned if the value of number is negative.
  • tan() – it returns the tangent of a number, where the numeric values represents the Tan of the angle.

NB. Remember the math methods given above have similar syntax as well as the examples are similar to the first two we had discussed above. We have summarized to avoid repeating the same thing but is good you try them in your text editor using different values and see the result.

Continuation of Math methods

There are other math methods here which have a different syntax form, therefore we will discuss them and give appropriate examples;

c) toSource()

It returns the string math, let’s have its syntax; Math.toSource( ); for better understanding let’s have an example;

<head>
<title>Example for toSource math method</title>
</head>
<body>
<script type="text/javascript">
var value = Math.toSource( );
document.write("Value is: " + value );
</script>
</body>

It will produce;
Value is: Math

d) max()

It returns the largest of zero or more numbers, it may result to -infinity if no arguments are given. Here is the syntax;

Math.max(value1, value2, … valueN);
syntax, where arguments in brackets are numbers

Let’s have an example here for better understanding;

<head>
<title>Example for max math method</title>
</head>
<body>
<script type="text/javascript">
var value = Math.max(40, 30, -1, 120);
document.write("First Test Value : " + value );
var value = Math.max(-2, -3, -60);
document.write("<br />Second Test Value : " + value );
var value = Math.max(0, -5);
document.write("<br />Third Test Value : " + value );
var value = Math.max(150);
document.write("<br />Fourth Test Value : " + value );
</script>
</body>




It will produce the following;
First Test Value : 120
Second Test Value : -2
Third Test Value : 0
Fourth Test Value : 150

Using the example above you may try min() math method, whereby it returns the smallest of zero or more numbers. +infinity is returned if no arguments are given, remember the syntax is similar.

e) atan2()

It returns the arctangent of the quotient of its arguments, values between -pi and pi are returned which represents the angle of an (x, y) point. Here is its syntax; Math.atan2(x, y); for better understanding let’s have an example;

<head>
<title>Example for atan math method</title>
</head>
<body>
<script type="text/javascript">
var value = Math.atan2(90,15);
document.write("First Test Value : " + value );
var value = Math.atan2(15,90);
document.write("<br />Second Test Value : " + value );
var value = Math.atan2(0, -0);
document.write("<br />Third Test Value : " + value );
var value = Math.atan2(+Infinity, -Infinity);
document.write("<br />Fourth Test Value : " + value );
</script>
</body>




It will produce;
First Test Value : 1.4056476672464
Second Test Value : 0.165148667276
Third Test Value : 3.141592y724353
Fourth Test Value : 2.356194568223

In order to keep focused in your dream to learn javascript try you own examples and thing of a project to develop, with the skills gained. Thanks a lot for reading through my tutorial, subscribe to our YouTube channel here;https://youtube.com/channel/UCHEbxRBbtkn11kPTIxnCERQ for live sessions and presentations. As you continue learning javascript hopefully to see you in our next tutorial.