Search code examples
javascripthtmloperator-keywordequalityinequalities

JavaScript operator for "within x of"


is there a JavaScript operator that will check proximity, kind of like a soft equals? For example, instead of if (a == b) it would read if (a is within 5 of b). There is probably no such thing but it would be a big help if there was. thank you


Solution

  • There's no built-in way to do this, but you can easily accomplish the same with this:

    if ( Math.abs(a - b) <= 5 )