Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run JavaScript using jsc on OS X?

I got this when I was trying to find some way to run my JavaScript programs through terminal. The run and load command mentioned can execute external JavaScript files. Help me how to do this. I am trying to run JavaScript programs which are store locally on my system.

EDIT: I am trying to solve Project Euler Q10 in JavaScript. So this is the program that I want to run in NodeJs or JSC. I need help in running the JavaScript files in Node and JSC. Any example will be really helpful.

Thank You all.

like image 866
SocialCircus Avatar asked Jul 26 '11 12:07

SocialCircus


People also ask

How do I run JavaScript on OSX?

In the Shortcuts app on your Mac, double-click a shortcut. In the shortcut editor, click at the top of the action list, begin typing “Run JavaScript…” in the search field, then double-click the Run JavaScript on Active Safari Tab action to add it to the shortcut editor.

How do I run a JavaScript file in Terminal Mac?

If you have already installed it, then simply open the terminal and type “node <FileName>. js”.

How do I run JavaScript?

To execute JavaScript in a browser you have two options — either put it inside a script element anywhere inside an HTML document, or put it inside an external JavaScript file (with a . js extension) and then reference that file inside the HTML document using an empty script element with a src attribute.

How do I run JavaScript code in Visual Studio Mac?

Open the JavaScript code file in Text Editor, then use shortcut Control + Alt + N (or ⌃ Control + ⌥ Option + N on macOS), or press F1 and then select/type Run Code , the code will run and the output will be shown in the Output Window.


2 Answers

Complements of this post, JSC lives at

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

and is not in the shell PATH by default. You can fix that with

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

jsc takes filenames as arguments. You could run a file named demo.js with

jsc demo.js 

Note that you'll have to use debug() instead of the conventional console.log() in your script to see any output.

like image 190
hurrymaplelad Avatar answered Sep 25 '22 06:09

hurrymaplelad


NOTE for MacOS Catalina (10.15.x) users

jsc now lives on a new path:

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

Make that adjustment to @hurrymaplelad's instructions and you'll be good to go.

like image 20
sherb Avatar answered Sep 26 '22 06:09

sherb