Javascript: Document about the restriction of Math.pow()?

Created on 28 Apr 2017  路  14Comments  路  Source: airbnb/javascript

Changelog said

prefer ** over Math.pow

But Airbnb JavaScript Style Guide didn't explain why.

question

Most helpful comment

I don't understand why one would prefere using ** over Math.pow
++ and -- or forbidden but ** is good?
In term of readability I prefere to see Math.pow a thousand time more than **

All 14 comments

We could add a section; but in general, syntax should always be preferred over API when they're equivalent.

Are you taking into account that ** operator is an ES2016 (ES7) feature, while the style guide is supposed to be ES2015 (ES6) compatible?

We are having troubles in our company with this, cause the linter is telling us that we should prefer ** over Math.pow, but Babel (with ES6 presets) doesn't parse it.

@eneko89 I'm not sure where you see that the style guide is limited to ES2015 - this is a JavaScript style guide. As soon as a feature is stage 4, and thus in the language, this guide may require it.

It's expected you're using https://npmjs.com/babel-preset-airbnb or the equivalent. Transforming ** is particularly trivial, so there's not really a good reason not to be using it.

We're having the same problem with @eneko89.

Would be better to have a ES7 section to specifically mention new ES7 syntaxes in README, just like what you did for ES6: ECMAScript 6+ (ES 2015+) Styles.

Note that the section is 2015+, not 2015. A PR to add a note about ** would be appreciated.

If you're using babel-preset-airbnb, it should just work - I suggest that.

I made a PR for the ** operator, feel free to have a look, thanks: https://github.com/airbnb/javascript/pull/1469

I don't understand why one would prefere using ** over Math.pow
++ and -- or forbidden but ** is good?
In term of readability I prefere to see Math.pow a thousand time more than **

@micky2be ** does not mutate a variable, it produces a new value - so yes, ++/-- is bad.

Math.pow is only more readable if you're familiar with its use in JS - ** is more common in other languages, so it's more readable overall.

Closed in #1469.

@ljharb if you ask Crowkford, it's better to avoid ++ because it makes fomula difficult to read.
This is why for me forcing the use of ** doesn't make sense to me.
The fact that it exists in other languages does not change that.

widht * height ** dist / time <- is it a typo or is this normal

Luckily we have another rule to force parens there, ensuring its readability.

My uglifyJS was actually crashing because I changed Math.pow() into *. Must be a Babel setup thing on my end... So how do disable this linting error for only *?

@sjorsvanheuveln if you're using https://npmjs.com/babel-preset-airbnb then you won't run into trouble here. It's far better to just transpile ** than to disable the linting error.

By the way for reference the setting is here if you really want, you can copy that and remove the parts you don't need

Was this page helpful?
0 / 5 - 0 ratings

Related issues

xgqfrms-GitHub picture xgqfrms-GitHub  路  3Comments

tunnckoCore picture tunnckoCore  路  3Comments

ryankask picture ryankask  路  3Comments

olalonde picture olalonde  路  3Comments

graingert picture graingert  路  3Comments