Java is a programming language, whereas JavaScript is essentially a scripting language. Developers use Java for building applications that can run in a virtual machine, an operating system or inside a browser. Contrastingly, JS code is meant to run inside a browser only.
Nonetheless, standalone desktop applications can be built with JavaScript by using Electron and Node.js. Another important distinction between Java and JS code is that while the former requires compilation, the latter is available in a text format.