DEV Community

Discussion on: Understanding the array methods Array.some() and Array.every() in JavaScript

Collapse
 
aminnairi profile image
Amin

Since these methods return a boolean, I would suggest a better way of naming the variables that holds the return values of those methods so that we understand quickly what values these variables are holding and why they are being used in the source-code.

// It reads almost as plain english and this comment is useless
const someoneIsLawyer = persons.some(({occupation}) => {
  return occupation === "Lawyer";
});
Enter fullscreen mode Exit fullscreen mode

Because I have an easier time reading this code.

// Reads as plain english again
if (someoneIsLawyer) {
  console.log("Do something if there is a lawyer.");
}
Enter fullscreen mode Exit fullscreen mode

Than this one.

// What does it hold? A boolean? Is it a function?
if (lawyerCheck) {
  console.log("Do something if there is a lawyer.");
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
sajithpradeep profile image
Sajith Pradeep

Thanks for this comment. I will definitely incorporate this next time around. :)

Collapse
 
monfernape profile image
Usman Khalil

Definitely makes sense. Boolean variables should has prefixes such as 'has, is' etc. in my opinion.