#!/usr/bin/env jsc
print("Hello, World!");
To execute such a JS file from the terminal, it's convenient to use the JSC that comes pre-installed on Mac.
The JavaScript interpreter on a Mac is located here:
/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc
Since it's not in the PATH by default, create a symbolic link in a directory that is in the PATH.
For example, you might use /usr/local/bin.
cd /usr/local/bin
ln -s /System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc ./
In the terminal, run:
jsc --help
If the help information is displayed, it's set up correctly.
Run:
jsc
This will start an interactive interface. The prompt and behavior are very similar to Python's interactive interface.
You can even write functions.
#!/usr/bin/env jsc
print("Hello, World!");
Save this as hello.js
.
chmod +x hello.js
./hello.js
Reference:
Comments