I recommend filtering your buffer/file through a node module called js-beautify (also available as a python module):
:%!js-beautify -f - " whole buffer
:'<,'>!js-beautify -f - " selected lines
Here is the custom command I have in after/ftplugin/javascript.vim
:
command! -buffer -range=% Format let b:winview = winsaveview() |
\ execute <line1> . "," . <line2> . "!js-beautify -f - -j -t -s " . &shiftwidth |
\ call winrestview(b:winview)
That command works on the whole buffer by default or on any range, while keeping the cursor position and, more or less, respecting indentation settings.
Read $ js-beautify --help
for the available options.