r/Web_Development • u/draikin3 • Jun 10 '20
How to simplify this statement ES6 style?
if (Number(this.score - this.scoreIntegerPart) !== 0) return Number(this.score - this.scoreIntegerPart);
else return y;
3
Upvotes
10
u/Friarchuck Jun 10 '20
const remainder = Number(this.score - this.scoreIntegerPart) return remainder !== 0 ? remainder : y
10
u/[deleted] Jun 10 '20
Unless I'm missing something...
The subtraction should coerce the result to a number which it returns if it's not zero (even if its negative) and when zero it evaluates to
falsey which then defaults toy2
u/justingolden21 Jun 10 '20
In addition to the other comments here, look up "ternary operator". It's basically if condition then x otherwise y. In other words
conditon ? x : y.You can just return the result, and if you're using ES6 per the title of the question, then you can use "arrow functions" if it's a one line statement with a return.
(x)=>2*xis a function that returns double the input, for example.