Install Prettier
Install the "Prettier - Code formatter" extension from the extensions sidebar. Once this is complete, open a code file and press Alt + Shift + F to auto format it. You should see a prompt asking you to choose which code formatter you would like to use since you now have two available. Select Prettier from the prompt.

Enable formatting on save for all files
Open the settings menu via "File > Preferences > Settings". Type editor.formatonsave into the search bar and enable the "Format On Save" setting. Now, whenever you save any file it should be formatted automatically.

Enable formatting on save for only certain file types
Open the settings.json file by navigating to "File > Preferences > Settings" and clicking the "Open Settings (JSON)" button at the top right of the screen.

Search the file for any preexisting "editor.formatOnSave" setting and delete it. Then add the following, replacing "javascript" with whatever language you want to enable format on save for.
"editor.formatOnSave": false,
"[javascript]": {
"editor.formatOnSave": true
}
Answer from Alex Myers on Stack Overflow