19

With OS X Xcode installed one can run ruby code in terminal with ">ruby somefile.rb" command (or from inside TextWrangler with the run command). You can also run a ruby interpreter from in terminal and type code in single lines while the interpreter retains variable objects.

Want same possibilities for Javascript. What must I install (if anything) on OS X to have that functionality? Not looking for an IDE as such just an interpreter that will run in Terminal.app?

I assume this is an allowed question for Stackoverflow based on other allowed questions about IDEs for various languages like: What's a good IDE for Python on Mac OS X?

6 Answers 6

39

There is a Javascript interpreter in the JavaScriptCore framework that comes with OS X. The interpreter is called jsc and can be found at the following path:

 /System/Library/Frameworks/JavaScriptCore.framework/Versions/Current/Helpers/jsc

There is a built-in function, quit(), which will exit interactive mode.

If you want to make it easier to use I suggest creating a symlink to a location in your path, e.g.:

sudo ln -s /System/Library/Frameworks/JavaScriptCore.framework/Versions/Current/Helpers/jsc /usr/local/bin

This will put a symbolic link in /usr/local/bin.

6
  • Thanks mttrb, any links to tutorials/docs for working with the JavaScriptCore framework outside Xcode (which I'm not overly familiar with)? Commented Apr 9, 2012 at 12:57
  • There isn't really much more to it than running jsc in Terminal.app and then typing JavaScript to run. Alternatively you can provide JavaScript in a file on the command line. jsc -h will give you the options. jsc has no relation to Xcode, the JavaScriptCore framework is the framework Safari uses for JavaScript.
    – mttrb
    Commented Apr 9, 2012 at 13:57
  • Thanks, to explain I'm used to seeing frameworks talked about in a Cocoa/Xcode contexts. Commented Apr 9, 2012 at 14:09
  • In El Capitan you'll get "Operation not permitted" when linking to /usr/bin because of rootless mode. I linked to /user/local/bin instead.
    – BoneGoat
    Commented Nov 9, 2015 at 13:25
  • On 10.13 High Sierra (and perhaps above) the jscbinary is moved to /System/Library/Frameworks/JavaScriptCore.framework/Versions/Current/Resources/jsc.
    – andiOak
    Commented Oct 17, 2022 at 12:11
18

Node.js. It's the V8 engine + libraries + REPL.

Although Node is usually used for web/network-related applications, at its core it's just a plain JS engine and can even be used for shell scripting.

You can install it from the installer, brew or just ./configure && make from a Node.js' tarball.

There's also Rhino.

5
  • Thanks porneL. With node.js installed, if I use Run command in TextWrangler will I get error line numbers returned on runtime fatal errors? Commented Apr 9, 2012 at 11:22
  • 1
    @wide_eyed_pupil I'm don't know how TextWrangler runs programs, but node will output error + line number + stack trace to stderr
    – Kornel
    Commented Apr 9, 2012 at 11:24
  • Thanks. Which install method would you recommend for a UNIX/bash newb like me? Commented Apr 9, 2012 at 12:54
  • node.js is one click download & install on OS X now nodejs.org Commented Jul 17, 2015 at 5:46
  • Hi, I think it is not fair to say about Rhino as a "dead project" - it works fine on OpenJDK and Android and is updated regularly - github.com/mozilla/rhino
    – alexkasko
    Commented Nov 5, 2017 at 23:49
2

v8. Its the javascript engine used in google chrome. You have to compile it for mac OS X, though. Theres a good tutorial here.

1
  • 1
    A simple way to install v8 on a Mac is with homebrew: brew install v8
    – peak
    Commented May 17, 2014 at 2:10
2

You have two options:

  1. Use console in your Browser: Browsers such as chrome,safari, firefox come buildtin with console which are cappable of running javascript. to open open console on chrome. Press CTRL + SHIFT + J

  2. Install Nodejs: As others have pointed out, try http://nodejs.org/ using which you can run javascript in terminal app similar to irb

1
  • Thanks looking for alternative to using Safari (currently using) and other environments (Apple's Quartz Composer) that happen to support JS. Commented Apr 9, 2012 at 13:00
1

On macOS Big Sur you can easily access you OS JavaScript interpreter by adding your local user binary folder. You can create a new symlink in /usr/local/bin with

% ln -s /System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Helpers/jsc /usr/local/bin/

Then you can access the OS's JavaScript interpreter by just typing

% jsc

Hint: Do not forget, that there is no console object, you can use debug(...) instead

0

On macOS 10.13 High Sierra the location for the jsc binary is:

/System/Library/Frameworks/JavaScriptCore.framework/Versions/Current/Resources/jsc

Using @mttrb's answer simply replace the path if you're on High Sierra. Seems like this binary has moved around a lot during macOS versions, so you would have to look around under the /System/Library/Frameworks/JavaScriptCore.framework/... path to find the location of jsc.

About documentation of jsc

The JavaScriptCore Swift/Objective-C framework of is a part of the Apple JavaScript additions for mac apps but the Apple Docs do very little to support the jsc binary. However, the original webkit docs do more:

https://trac.webkit.org/wiki/JSC

Here you will find the functions mentioned in other answers here, like the debug() (replaces console.log()), the quit() (that exits terminal interactive shell mode) and so on.

Other JavaScript shells

Here is a list of a couple of JS shells, many of which are cross-platform and can be installed using a single pre-compiled binary (such as JSDB):

https://reference.codeproject.com/javascript/shells

Not the answer you're looking for? Browse other questions tagged or ask your own question.