The Math object is a predefined JavaScript object used to perform mathematical operations. It’s also used to obtain the values of predefined mathematical constants, i.e., Pi (π) or the square root of a number. In addition, it can also be used to generate random numbers.
It’s important to remember that the Math object begins with an uppercase “M.” If a lower case “m” is used, the JavaScript interpreter will not recognize it as an object.
(Note: We won’t cover all of these properties and methods in detail as some of them are a little more complicated than what we are dealing with right now.)
Properties
Name
Description
E
The value of Euler’s constant, approximately 2.718
LN2
The natural logarithm of 2, approximately 0.693
LN10
The natural logarithm of 10, approximately 2.302
LOG2E
The base 2 logarithm of E, approximately 1.442
LOG10E
The base 10 logarithm of E, approximately 0.434
PI (π)
The ratio of the circumference of a circle to its diameter, approximately 3.14159
SQRT1_2
Square root of 1/2; equivalently, 1 over the square root of 2, approximately 0.707
SQRT2
Square root of 2, approximately 1.414
Advertisement
Using Pi
You can use this script to calculate the circumference of a circle using Pi by entering the radius:
Let’s see what we did in this script.
First, we created a function called circum.
Then, we declared the variable radius and gave it the value of the results of a prompt box, which obtains the size of the radius from the user.
Next, we declared the variable circle and gave it the formula which will calculate the actual circumference of the circle, based on the information given to the radius variable.
Finally, an alert box displays the results of the calculation.
You would then add this in the body section of the page in order to call the script above:
Let’s take a look at the JavaScript math methods and see what we can do with them.
Introduced in 2015, ES6 (also known as ECMAScript 2015 or ECMAScript 6) was the most impactful revision of JavaScript since ES5 (ECMAScript 5) in 2009. It added a veritable cornucopia of features, including Arrow Functions, the const and let keywords, default parameters, among others. Then ECMAScript 2016, 2017, and 2018 added even more, including the […]
If you have ever examined an Angular app’s file structure, you would find a lot of HTML, CSS, and JavaScript templates that all work together and get compiled to render a well-crafted web application. The compilation process consists of two approaches. The first one is the Just in Time (JIT) compilation, on which Angular relied […]
Sometimes it becomes necessary to convert an object into a string in order to view or store it. For the purpose of debugging, a simple console.log() will suffice, as just about any modern browser’s Dev Tools will provide a pretty printed version of an object. Meanwhile, relying on implicit conversion when writing an object to […]
All programming languages, including JavaScript (JS), have built-in data types; these include strings, numbers, Dates, Arrays, and so forth. What makes JS special is that it is loosely typed. Hence, variables are declared without a type, which allows them to be re-assigned values of a different type at any time. This feature makes the language […]
The original home of HTML tutorials. HTMLGoodies is a website dedicated to publishing tutorials that cover every aspect of being a web developer. We cover programming and web development tutorials on languages and technologies such as HTML, JavaScript, and CSS. In addition, our articles cover web frameworks like Angular and React.JS, as well as popular Content Management Systems (CMS) that include WordPress, Drupal, and Joomla. Website development platforms like Shopify, Squarespace, and Wix are also featured. Topics related to solid web design and Internet Marketing also find a home on HTMLGoodies, as we discuss UX/UI Design, Search Engine Optimization (SEO), and web dev best practices.
Advertiser Disclosure: Some of the products that appear on
this site are from companies from which TechnologyAdvice
receives compensation. This compensation may impact how and
where products appear on this site including, for example,
the order in which they appear. TechnologyAdvice does not
include all companies or all types of products available in
the marketplace.