JSC is the JavaScript engine from Apple's JavaScriptCore (WebKit) as a console application that you can use to run script in the terminal.
For more info visit the JSC's webkit wiki page.
Using jsc is simple, the one issue is that Apple keeps changing the location for jsc. To deal with this issue I just create a symbolic link to the binary:
- Find where jsc is at:
find /System/Library/Frameworks/JavaScriptCore.framework -iname jsc
In Catalina and BigSur the above command will return: /System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Helpers/jsc
.
- Add a symbolic link to the jsc binary you located with
find
:
ln -s /System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Helpers/jsc /usr/local/bin
If you already have a symbolic you need to delete it first with:
rm /usr/local/bin/jsc
.
- Now you can just type
jsc
in the terminal!
JSC adds the function print()
to write to the terminal.
The function load('[FileName]')
lets you load and execute the an external JavaScript file. If the run is successful, it will return the final value of the script.
JSC does not support console.log()
, use the function debug()
instead.
I usually add a boilerplate for console.log
.
var console = {log : debug};
I also have a dump()
function to help me debug in JSC. You can include this function by calling load('./util_dump.js')
.
This is my old shell script, no longer used since it changes so often.
#!/usr/bin/env bash
if [ -f /usr/lib/jsc ]; then
/usr/lib/jsc "$@"
elif [ -f /System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc ]; then
/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc "$@"
elif [ -f /System/Library/Frameworks/JavaScriptCore.framework/Resources/jsc ]; then
/System/Library/Frameworks/JavaScriptCore.framework/Resources/jsc "$@"
fi