vs code autocomplete when I write a script in HTML itself but when I make a separate file for javascript it does not work
JavaScript isn't necessarily used with an HTML DOM, so when writing a separate js file, VS Code does not assume a DOM to be present by default, and it therefore does not show you autocompletions for that.