How to handle this case in javascript?