refactoring js code on frontend