Teachnique
      CourseRoadmaps
      Login

      OverviewPlacementSyntaxHello WorldConsole.log()CommentsVariableslet StatementConstantsData TypesType ConversionsStrict ModeReserved Keywords

      OperatorsArithmetic OperatorsComparison OperatorsLogical OperatorsBitwise OperatorsAssignment OperatorsConditional Operatorstypeof OperatorNullish Coalescing OperatorDelete OperatorComma OperatorGrouping OperatorYield OperatorSpread OperatorExponentiation OperatorOperator Precedence

      If...ElseWhile LoopsFor LoopFor...in LoopFor...of LoopLoop ControlBreak StatementContinue StatementSwitch CaseUser Defined Iterators

      FunctionsFunction ExpressionsFunction ParametersDefault ParametersFunction() ConstructorFunction HoistingArrow FunctionsFunction InvocationFunction call() MethodFunction apply() MethodFunction bind() MethodClosuresVariable ScopeGlobal VariablesSmart Function Parameters

      NumberBooleanStringsArraysDateMathRegExpSymbolSetsWeakSetMapsWeakMapIterablesReflectTypedArrayTempate LiteralsTagged Templates

      Objects OverviewClassesObject PropertiesObject MethodsStatic MethodsDisplay ObjectsObject AccessorsObject ConstructorsNative PrototypesES5 Object MethodsEncapsulationInheritanceAbstractionPolymorphismDestructuring AssignmentObject DestructuringArray DestructuringNested DestructuringOptional ChainingGlobal ObjectMixinsProxies

      HistoryVersionsES5ES6ECMAScript 2016ECMAScript 2017ECMAScript 2018ECMAScript 2019ECMAScript 2020ECMAScript 2021ECMAScript 2022

      CookiesCookie AttributesDeleting Cookies

      Browser Object ModelWindow ObjectDocument ObjectScreen ObjectHistory ObjectNavigator ObjectLocation ObjectConsole Object

      Web APIHistory APIStorage APIForms APIWorker APIFetch APIGeolocation API

      EventsDOM Events

      Feedback

      Submit request if you have any questions.

      Course
      Math

      JavaScript Tutorial

      This JavaScript tutorial is crafted for beginners to introduce them to the basics and advanced concepts of JavaScript. By the end of this guide, you'll reach a proficiency level that sets the stage for further growth. Aimed at empowering you to progress towards becoming a world-class software developer, this tutorial paves the way for a successful career in web development and beyond.

      Math Object

      The math object provides you properties and methods for mathematical constants and functions. Unlike other global objects, Math is not a constructor. All the properties and methods of Math are static and can be called by using Math as an object without creating it.
      Thus, you refer to the constant pi as Math.PI and you call the sine function as Math.sin(x), where x is the method's argument.

      Syntax

      The syntax to call the properties and methods of Math are as follows
      var pi_val = Math.PI; // Property
      var sine_val = Math.sin(30); // Method

      Math Properties

      Here is a list of all the properties of Math and their description.
      Sr.No.
      Property & Description
      1
      AI
      Euler's constant and the base of natural logarithms, approximately 2.718.
      2
      AI
      Natural logarithm of 2, approximately 0.693.
      3
      AI
      Natural logarithm of 10, approximately 2.302.
      4
      AI
      Base 2 logarithm of E, approximately 1.442.
      5
      AI
      Base 10 logarithm of E, approximately 0.434.
      6
      AI
      Ratio of the circumference of a circle to its diameter, approximately 3.14159.
      7
      AI
      Square root of 1/2; equivalently, 1 over the square root of 2, approximately 0.707.
      8
      AI
      Square root of 2, approximately 1.414.
      In the following sections, we will have a few examples to demonstrate the usage of Math properties.

      Math Methods

      Here is a list of the methods associated with Math object and their description
      Sr.No.
      Method & Description
      1
      AI
      Returns the absolute value of a number.
      2
      AI
      Returns the arccosine (in radians) of a number.
      3
      AI
      Returns the inverse hyperbolic consine of a number.
      4
      AI
      Returns the arcsine (in radians) of a number.
      5
      AI
      Returns the inverse hyperbolic sine of a number.
      6
      AI
      Returns the arctangent (in radians) of a number.
      7
      AI
      Returns the arctangent of the quotient of its arguments.
      8
      AI
      Returns the inverse hyperbolic tangent of a number.
      9
      AI
      Finds a cube root of a given number.
      10
      AI
      Returns the smallest integer greater than or equal to a number.
      11
      AI
      Returns the number of leading zero in 32-bit binary number.
      12
      AI
      Returns the cosine of a number.
      13
      AI
      It returns the hyperbolic cosine of a number.
      14
      AI
      Returns EN, where N is the argument, and E is Euler's constant, the base of the natural logarithm.
      15
      AI
      Returns EN - 1, where N is the argument, and E is Euler's constant, the base of the natural logarithm.
      16
      AI
      Returns the largest integer less than or equal to a number.
      17
      AI
      Returns a nearest 32-bit single precision float representation of the number.
      18
      AI
      Calculates the square root of the sum of squares of arguments.
      19
      AI
      Calculates the 32-bit multiplication of parameters.
      20
      AI
      Returns the natural logarithm (base E) of a number.
      21
      AI
      Returns the logarithm (base 10) of a number.
      22
      AI
      Return the natural logarithm (base E) of 1 + N, where N is an argument.
      23
      AI
      Returns the base 2 logrithm of a number.
      24
      AI
      Returns the largest of zero or more numbers.
      25
      AI
      Returns the smallest of zero or more numbers.
      26
      AI
      Returns base to the exponent power that is, base exponent.
      27
      AI
      Returns a pseudo-random number between 0 and 1.
      28
      AI
      Returns the value of a number rounded to the nearest integer.
      29
      AI
      Return -1 or 1 indicating the sign of the number.
      30
      AI
      Returns the sine of a number.
      31
      AI
      Return the hyperbolic sin.
      32
      AI
      Returns the square root of a number.
      33
      AI
      Returns the tangent of a number.
      34
      AI
      Returns the hyperbolic tangent of the number.
      35
      AI
      Returns the integer part of the number.
      In the following sections, we will have a few examples to demonstrate the usage of the methods associated with Math.

      Examples

      Example: Math Properties

      The example below demonstrates that each property of the Math object has a constant value.
      Here, we have accessed the values of the 'E', 'LN2’, and 'PI', etc., properties.
      <html>
      <head>
      <title> JavaScript - Math object's properties </title>
      </head>
      <body>
      <p id = "output"> </p>
      <script>
      document.getElementById("output").innerHTML =
      "Math.E == " + Math.E + "<br>" +
      "Math.LN2 == " + Math.LN2 + "<br>" +
      "Math.LN10 == " + Math.LN10 + "<br>" +
      "Math.PI == " + Math.PI + "<br>"+
      "Math.LOG2E == " + Math.LOG2E + "<br>" +
      "Math.LOG10E == " + Math.LOG10E;
      </script>
      </body>
      </html>

      Output

      After executing the above program, it returns the values of the provided Math properties.
      Math.E == 2.718281828459045
      Math.LN2 == 0.6931471805599453
      Math.LN10 == 2.302585092994046
      Math.PI == 3.141592653589793
      Math.LOG2E == 1.4426950408889634
      Math.LOG10E == 0.4342944819032518

      Example: Math ceil() method

      Here, we are computing the JavaScript ceil() method to return the smallest larger integer value than the number passed as an argument. Here, the method returns 6 for the 5.9 value.
      <html>
      <head>
      <title> JavaScript - Math.ceil() method </title>
      </head>
      <body>
      <p id = "output"> </p>
      <script>
      let ans = Math.ceil(5.9);
      document.getElementById("output").innerHTML =
      "Math.ceil(5.9) = " + ans;
      </script>
      </body>
      </html>

      Output

      After executing the above program, it returns the result as 6.
      Math.ceil(5.9) = 6

      Example: Math max() method

      The Math.max() method is used to get the maximum value among the arguments passed as an array.
      Here, we have passed six arguments to the Math.max() object, and the method returns the maximum value from them.
      <html>
      <head>
      <title> JavaScript - Math.max() method </title>
      </head>
      <body>
      <p id = "output"> </p>
      <script>
      let ans = Math.max(100, 10, -5, 89, 201, 300);
      document.getElementById("output").innerHTML =
      "Math.max(100, 10, -5, 89, 201, 300) = " + ans + "<br>";
      </script>
      </body>
      </html>

      Output

      After executing the above program, it returns 300 as maximum value.
      Math.max(100, 10, -5, 89, 201, 300) = 300

      Example: Math cos() method

      The Math.cos() method returns the cosine value of the number passed as an argument. The cosine value of 0 is 1, which you can see in the output of the example below.
      <html>
      <head>
      <title> JavaScript - Math.cos() method </title>
      </head>
      <body>
      <p id = "output"> </p>
      <script>
      let ans = Math.cos(0);
      document.getElementById("output").innerHTML = "Math.cos(0) = " + ans;
      </script>
      </body>
      </html>

      Output

      If we execute the above program, it returns "1" as result.
      Math.cos(0) = 1