简体   繁体   中英

Can't run my JavaScript code directly on system

I am unable to run this javascript code in the terminal even though the code is correct! I am using visual studio. and it runs perfectly when I click on run code but doesn't run in terminal

错误捕捉

You can't execute your JavaScript that way because your computer does not know how to.

(Computer) code can only get executed in a certain environment. E. g.Java needs the JRE (Java Runtime Environment), and JavaScript needs its own environment, too.


One environment would be your webbrowser (what JavaScript was initially designed for).

So if I execute JavaScript here in the webbrowser, it works! Why? Because it has a JavaScript engine (the environment JavaScript needs to be executed in) already built in.

 const sayHello = name => { return "hey there " + name + "!"; }; console.log(sayHello("John"));


If you want to execute JavaScript directly on your system like you tried, you will need to install such an evironment like your browser already has. Node.js would be such an option.

After you have installed it on your system, you can run node your-file.js and you will see your expected output on the console.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM