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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

  • Batching Strategies: Throughput vs Latency in Practice
  • Data Integration Landscape 2026: Event Streaming, API, and Batch in the Era of Agentic AI
  • How to Format Articles for DZone
  • How Kestra Unifies Enterprise Workflow Orchestration and Agentic AI

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Feb. 10, 16 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
7.8K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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. 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

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook