DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How To Create a Network Graph Using JavaScript
  • How To Create a Resource Chart in JavaScript
  • Prototype Pattern in JavaScript
  • How to Build a Treemap Using JavaScript

Trending

  • The Modern Data Stack Is Overrated — Here’s What Works
  • Unlocking AI Coding Assistants Part 2: Generating Code
  • Java’s Next Act: Native Speed for a Cloud-Native World
  • It’s Not About Control — It’s About Collaboration Between Architecture and Security
  1. DZone
  2. Coding
  3. Languages
  4. ES6 Math Functions You Can Use Today

ES6 Math Functions You Can Use Today

In this article, we'll discuss the various math functions—what they are and how they work—available in ECMAScript 2015 (ES6). Read on to learn more.

By 
Josh Anderson user avatar
Josh Anderson
·
Feb. 10, 16 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
7.3K Views

Join the DZone community and get the full member experience.

Join For Free

as of june 2015, the world has a new javascript standard. officially called ecmascript 2015, but commonly known as ecmascript 6 (es6) and sometimes ecmascript harmony or ecmascript.next, the specifications detail a broad range of new features you can expect to see being implemented in browsers in the coming months and years. this includes—but is not limited to—block-level scoped variables let and const, new class syntax, default parameters, modules, sets, and template strings. it also includes some useful additions to the math object. unlike most new features of es6 at the time of writing, (almost in their entirety) these features have already been implemented in the desktop versions of chrome, firefox, safari, and opera, as well as microsoft edge (though not internet explorer). support in mobile browsers is weaker.

none of these new additions are anything groundbreaking. in fact, it's more about adding basic math functionality that is already present in most popular languages. i still think it's worth taking a look at some of them as they will likely be overlooked in other guides to es6. mdn has simple polyfills for all of them, so if you want to use them on your production site you can, even if you get significant mobile traffic.

the idea of math.trunc is simple—it removes the digits after the decimal point from a number. 13.2 becomes 13 and -12.6 becomes -12. this is similar to math.ceil and math.floor, except we're not always rounding up or always rounding down, we're rounding towards zero. that is, if the number is positive then math.trunc rounds down but if it is negative it'll round up. the chart below shows how math.trunc and the other rounding function, math.round, work over the range -5 to 5. (plotting math.floor, math.ceil, and math.trunc in the same chart is confusing.)

math.sign is even simpler. pass in a number and it will return 1 if the number is positive (even if that "number" is infinity) and -1 if it is negative (including -infinity). if the number is zero then it will return 0 (or -0, which is the same).

math.cbrt returns the cube root of a number. for positive x, math.cbrt(x) returns the same result as math.pow(x,1/3). however, if x is negative the latter returns nan where as the former will return the same result as -1*math.pow(-x,1/3). for example, math.cbrt(-8) returns -2 while math.pow(-8,1/3) returns nan. this isn't a bug , it's part of the specification . at the same time -2, is a correct solution; all non-zero real numbers, positive and negative, have three cubed roots but two of them are complex . the chart below shows curves for math.cbrt and the (old) square root function math.sqrt. (the square roots of all negative numbers are complex so math.sqrt also returns nan when fed a negative argument.)

javascript has had the math.log function, that calculates the natural logarithm (that is base-e) of its argument, since the 1st edition of ecmascript. es6 adds math.log10 and math.log2 that calculate the base-10 and base-2 (a.k.a. binary) logarithms of their argument. the former is frequently used in 2d data visualization in the sciences when the numbers being plotted span a large range in one or both dimensions. confusingly, many math text books will use "log" to signify the base-10 logarithm, shortening the natural logarithm to "ln".

es6 also sees the introduction of math.log1p, which is the same as math.log(1 + x), and math.expm1, which produces the same result as math.exp(x)-1. the chart directly below plots the aforementioned logarithmic functions while the one below that plots math.exp and math.expm1.

math.hypot calculates the square root of the sum of the squares of its arguments. obviously you can use it to find the hypotenuse of a right triangle if you know the lengths of the other two sides, but you can pass it more than two numbers, or just 1, and it will still calculate the sum of the squares of its arguments. the chart below, for example, shows the result of calling math.hypot for all non-negative integers up to x. for example, the y-value at x=1 is the result of math.hypot(0,1), at x=2 it's the result of math.hypot(0,1,2), at x=3 it's the result of math.hypot(0,1,2,3) and so on.

finally, es6 sees the addition of the hyperbolic functions sinh, cosh, and tanh and their inverses asinh, acosh, and atanh. hyperbolic functions are analogs of their similarly named trigonometric counterparts (sin, cos, etc.) and crop up regularly in physics, engineering, and architecture. the chart immediately below plots math.sinh, math.cosh, and math.tanh; the one below that plots their inverses.

there are three further functions added to the math object that, for reasons of brevity, i won't discuss. they are math.imul , math.fround , and math.clz32 .

originally written by tim brock

Chart IT Data visualization ECMAScript Microsoft Edge JavaScript Pass (software) mobile Object (computer science) Template

Published at DZone with permission of Josh Anderson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How To Create a Network Graph Using JavaScript
  • How To Create a Resource Chart in JavaScript
  • Prototype Pattern in JavaScript
  • How to Build a Treemap Using JavaScript

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!