Convert to arrow function

Convert to arrow function
0.0 0


Hi campers,

I have code as below (examine the array, takes only integer (10.0 count as integer) and get rid off the decimal entry(5.6, -9.8, 3.14, 8.34):

const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34];

function checkNumber(num) {
  let remainder = num % 1;
  return remainder === 0;

let realNumberClean = realNumberArray.filter(checkNumber);

How do I convert my code above to use arrow function?


let CheckNumber = (num) => {
  let remainder = num % 1;
  return remainder === 0;

Usually arrow functions aren’t used that way though, arrow functions are a way to make your code clearer, a simple function should just be declared as it has always been, because it’s easier to identify purpose that way.
A callback on the other hand, is much easier to read as an arrow function. If you want to assign your function to a variable arrows can be useful, too.

Generally always do what makes your code clearer, not what is new or even more efficient (considering that efficiency isn’t essential for your project), readability is one of the most important characteristics a piece of code can have.


Given that your only using the function for a filter, you could simplify it like so:

realNumArray.filter ( (cv)=>{ return !(cv%1);  } );
// or even simpler
realNumArray.filter ( cv=> !(cv%1) );

In general, I only use arrow functions in cases like this. I would never name an arrow function like @GregoryGoncalves shows above. However I’m no expert. Hope that helps.