How to write cleaner code with JavaScript