Replace Equality Expressions with indexOf() in JavaScript

Simplify your conditions and improve readability

Jonathan Hsu
Code 85
Published in
2 min readFeb 11, 2022

--

Photo by Chase Clark on Unsplash

Let’s put ourselves in a fictitious scenario where a message prints only during the hours of 8AM, 10AM, and 2PM. The code block might look something like this…

const d = new Date();
const hour = d.getHours() + 1;
if(hour === 8 || hour…

--

--