JavaScript cleanup and conditional check implementation