Let's first say that unless you were in the design discussions for Javascript in its early days, none of us actually "know" why. The best we can do is try to infer why certain choices might have been made given the objectives they had and the choices they had.
If you look at the requirements for the original design of Javascript in web pages, you see things like this:
- Must run on lots of platforms.
- Must be easily embeddable in HTML pages.
- Must be simple to program.
- Is not initially an environment that feels the need to maximize execution performance.
- Not Java.
Let's look at these...
About #1, OK, run on lots of platforms means it cannot be compiled to native machine code - period. It could be compiled to a universal byte code like Java or webAssembly, but read on about the other requirements.
About #2, it wants to be embeddable in web pages so you can do things like:
<div onclick='alert("hi")'>Click me</div>
Then, it's pretty hard to have code that is compiled in advance fit in there. You'd probably have to compile your whole web page. That creates an entirely new paradigm and browser (that expects pre-compiled code and HTML). While the world could have eventually gone there, that certainly wasn't an easy way to go (requiring a redo of the browser).
About #3, "simple to program", it's generally believed that interpreted "scripts" are simpler for people to start with than languages that need a programming environment and compiler set up and some build tools. It's faster and simpler to do simple things.
About #4, "performance". In the early days of Javascript, it was an auxiliary language to help add some client-side logic to web pages. The initial target was far simpler than what Javascript is being used for today. I rather doubt it was envisioned that a pre-compiled language was needed for what its initial target was. So, keep it simple and go with the simpler way of reaching your target.
About #5: "not Java". Java was a known tool of the day. But, Java was not super simple, required pre-compiling, had IP encumbrances, etc... So, Javascript was born to be something that was familiar to both C and Java developers, but was far simpler for someone new to pick up. Easy to do simple things.
As for environments like nodejs, they could more practically have a pre-compile step, but the early designers of nodejs decided to use the open source V8 Javascript engine rather than make their own Javascript engine. Plus, in a server world, your code is generally loaded once at server startup where V8 compiles it to a combination of native code and byte code anyway so requiring developers to pre-compile it doesn't necessarily buy you a lot anyway. This is where it matters that Javascript is now actually compiled, it's just compiled upon loading rather than requiring pre-compiling by the developer.
And, nowadays, if you want the benefits of type checking in a pre-compile step, you can use TypeScript and precompile that to Javascript.