When testing JavaScript code, I usually write an HTML page, place the code in the body, execute it in the browser, and then check the JavaScript console. Suddenly, it occurred to me that I could use a JavaScript engine to execute JS directly. So, I decided to give it a try.
After a quick Google search, I found Mozilla’s SpiderMonkey engine and Google’s V8 engine. Since V8 is widely recognized for its speed, I decided to go with it. However, after looking at some online articles, I realized that installing the V8 engine is quite troublesome = .=
Then I remembered that Node.js seems to use the V8 engine, so I could just use Node.js to execute JS.
I went to the Node.js official website to download the latest version. There is a pkg installer for Mac, and after installation, I needed to configure Vim to use Node.js to execute JS files. Next, I modified the .vimrc file.
In the .vimrc file, I added a compile command for JavaScript files: node filename.js, as shown in the image.
With these steps completed, you can now run JavaScript directly in Vim.