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?

Community
  • 1
  • 1
wide_eyed_pupil
  • 3,153
  • 7
  • 24
  • 35

6 Answers6

38

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.

mttrb
  • 8,297
  • 3
  • 35
  • 57
  • Thanks mttrb, any links to tutorials/docs for working with the JavaScriptCore framework outside Xcode (which I'm not overly familiar with)? – wide_eyed_pupil Apr 09 '12 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 Apr 09 '12 at 13:57
  • Thanks, to explain I'm used to seeing frameworks talked about in a Cocoa/Xcode contexts. – wide_eyed_pupil Apr 09 '12 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 Nov 09 '15 at 13:25
  • On 10.13 High Sierra (and perhaps above) the `jsc`binary is moved to `/System/Library/Frameworks/JavaScriptCore.framework/Versions/Current/Resources/jsc`. – andiOak Oct 17 '22 at 12:11
  • @andiOak That is the old location for `jsc`, this answer actually used to have that path but was updated when macOS moved it into `Helpers`. I just checked and it is still in `Helpers` on macOS Monterey (12.6). – mttrb Oct 24 '22 at 08:10
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.

Kornel
  • 97,764
  • 37
  • 219
  • 309
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.

pbfy0
  • 616
  • 4
  • 13
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

CuriousMind
  • 33,537
  • 28
  • 98
  • 137
  • Thanks looking for alternative to using Safari (currently using) and other environments (Apple's Quartz Composer) that happen to support JS. – wide_eyed_pupil Apr 09 '12 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

Binarian
  • 12,296
  • 8
  • 53
  • 84
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

andiOak
  • 356
  • 3
  • 9