Exforsys

JavaScript Tutorial

  1. JavaScript Browser Objects Part 2
  2. JavaScript Frame object
  3. JavaScript Form Object
  4. JavaScript FileUpload Object
  5. JavaScript Event Object Properties and Methods
  6. JavaScript Event Object
  7. JavaScript Elements and Embed Objects
  8. JavaScript Applet Objects
  9. JavaScript Browser Objects
  10. JavaScript Object Oriented Features
  11. JavaScript Window Object Open Method Part 2
  12. JavaScript Window Object Open Method
  13. JavaScript Window Object Timeout Methods
  14. JavaScript Location Object
  15. JavaScript Location Object Properties
  16. JavaScript History Object Properties and Methods
  17. JavaScript Document Object Methods Part II
  18. JavaScript Document Object Methods Part I
  19. JavaScript Document Object Properties
  20. JavaScript Document Object
  21. JavaScript Windows Object Properties Part II
  22. JavaScript Windows Object Properties Part I
  23. JavaScript DOM Window Object
  24. Working with JavaScript DOM Objects
  25. JavaScript Array Object Methods – Part II
  26. JavaScript Array Object
  27. JavaScript Array Object Methods – Part I
  28. JavaScript Boolean Object
  29. JavaScript OnError Event
  30. JavaScript Exception Handling – Part II
  31. JavaScript Exception Handling – Part I
  32. JavaScript Event Handler
  33. JavaScript Events Handling
  34. JavaScript Array Operations
  35. JavaScript Two Dimensional Arrays
  36. Passing values to JavaScript Function
  37. JavaScript Functions
  38. JavaScript Arrays
  39. JavaScript Iterative Structures - Part II
  40. JavaScript Iterative Structures - Part I
  41. JavaScript Math Object
  42. JavaScript Date Object
  43. JavaScript String Object
  44. JavaScript Objects
  45. JavaScript Confirm Box
  46. JavaScript Alert Box
  47. JavaScript Conditional Statements Part 2
  48. JavaScript Conditional Statements Part 1
  49. How to use JavaScript in HTML page
  50. JavaScript Variables
  51. JavaScript Features
  52. JavaScript Introduction

Ads


Home arrow Technical Training arrow JavaScript Tutorial

JavaScript Math Object

Author : Exforsys Inc.     Published on: 9th Aug 2007

JavaScript Math Object

In this JavaScript tutorial, you will learn about Math Object, usage, properties and methods of math object along with syntax and examples.

Ads

Usage of Math Object:

JavaScript Math object is used to perform mathematical tasks. But unlike the String and the Date object which requires defining the object, Math object need not be defined.  Math object in JavaScript has two main attributes:

  • Properties
  • Methods

Properties of Math Object:

The JavaScript has eight mathematical values and this can be accessed by using the Math Object. The eight mathematical values are:

  • E
  • PI
  • square root of 2 denoted as SQRT2
  • square root of 1/2 denoted as SQRT1_2
  • natural log of 2 denoted as LN2
  • natural log of 10 denoted as LN10
  • base-2 log of E denoted as LOG2E
  • base-10 log of E denoted as LOG10E

The way of accessing these values in JavaScript is by using the word Math before these values namely as

  • Math.E
  • Math.LOG10E and so on

Methods of Math Object:

There are numerous methods available in JavaScript for Math Object. Some of them are mentioned below namely:

  • abs(x) - Returns absolute value of x.
  • acos(x) - Returns arc cosine of x in radians.
  • asin(x) - Returns arc sine of x in radians.
  • atan(x) - Returns arc tan of x in radians.
  • atan2(y, x) - Counterclockwise angle between x axis and point (x,y).
  • ceil(x) - Returns the smallest integer greater than or equal to x. (round up).
  • cos(x) - Returns cosine of x, where x is in radians.
  • exp(x) - Returns ex
  • floor(x) - Returns the largest integer less than or equal to x. (round down)
  • log(x) - Returns the natural logarithm (base E) of x.
  • max(a, b) - Returns the larger of a and b.
  • min(a, b) - Returns the lesser of a and b.
  • pow(x, y) - Returns xy
  • random() - Returns a pseudorandom number between 0 and 1.
  • round(x) - Rounds x up or down to the nearest integer. It rounds .5 up.
  • sin(x) - Returns the Sin of x, where x is in radians.
  • sqrt(x) - Returns the square root of x.
  • tan(x) - Returns the Tan of x, where x is in radians.

Example for Math Object methods mentioned above:


<html>
   <
body>
      <
script type="text/javascript">
         
document.write(Math.round(5.8))

      </script>
   </
body>
</
html>

The output of the above program is

6

This is because the round() method rounds the number given in argument namely here 5.8 to the nearest integer. It rounds .5 up which gives 6.

Another example for using Math Object in JavaScript.


<html>
   <
body>
      <
script type="text/javascript">
          document.write(Math.max(8,9) + "<br />")
          document.write(Math.max(-5,3) + "<br />")
          document.write(Math.max(-2,-7) + "<br />")

      </script>
   </
body>
</
html>

Ads

Output of the above program is

9
3
-2

The above example uses the max() method of the Math object which returns the largest of the two numbers given in arguments of the max method.



 
This tutorial is part of a JavaScript Tutorial tutorial series. Read it from the beginning and learn yourself.

JavaScript Tutorial

  1. JavaScript Browser Objects Part 2
  2. JavaScript Frame object
  3. JavaScript Form Object
  4. JavaScript FileUpload Object
  5. JavaScript Event Object Properties and Methods
  6. JavaScript Event Object
  7. JavaScript Elements and Embed Objects
  8. JavaScript Applet Objects
  9. JavaScript Browser Objects
  10. JavaScript Object Oriented Features
  11. JavaScript Window Object Open Method Part 2
  12. JavaScript Window Object Open Method
  13. JavaScript Window Object Timeout Methods
  14. JavaScript Location Object
  15. JavaScript Location Object Properties
  16. JavaScript History Object Properties and Methods
  17. JavaScript Document Object Methods Part II
  18. JavaScript Document Object Methods Part I
  19. JavaScript Document Object Properties
  20. JavaScript Document Object
  21. JavaScript Windows Object Properties Part II
  22. JavaScript Windows Object Properties Part I
  23. JavaScript DOM Window Object
  24. Working with JavaScript DOM Objects
  25. JavaScript Array Object Methods – Part II
  26. JavaScript Array Object
  27. JavaScript Array Object Methods – Part I
  28. JavaScript Boolean Object
  29. JavaScript OnError Event
  30. JavaScript Exception Handling – Part II
  31. JavaScript Exception Handling – Part I
  32. JavaScript Event Handler
  33. JavaScript Events Handling
  34. JavaScript Array Operations
  35. JavaScript Two Dimensional Arrays
  36. Passing values to JavaScript Function
  37. JavaScript Functions
  38. JavaScript Arrays
  39. JavaScript Iterative Structures - Part II
  40. JavaScript Iterative Structures - Part I
  41. JavaScript Math Object
  42. JavaScript Date Object
  43. JavaScript String Object
  44. JavaScript Objects
  45. JavaScript Confirm Box
  46. JavaScript Alert Box
  47. JavaScript Conditional Statements Part 2
  48. JavaScript Conditional Statements Part 1
  49. How to use JavaScript in HTML page
  50. JavaScript Variables
  51. JavaScript Features
  52. JavaScript Introduction
 

Comments