JavaScript Shell and More

D

DevynCJohnson

Guest
As many computer users know, a variety of scripting languages exist. Such scripting languages can be run in executable files called scripts. Commonly executed scripts may be written in Python, Ruby, Perl, BASH (shell), etc. Also, these same languages have interactive interpreters that allow users to type a command and execute it immediately. However, JavaScript is a popular scripting language that appears to only run in a web-browser as part of the webpage. Perhaps, JavaScript is no different than the other previously mentioned languages.

"jsc" is a command-line tool that allows users to execute a JavaScript as if it were any other type of script. Open a terminal and type "jsc" and then the path to the JavaScript file. If no file or path is specified, then an interactive JavaScript shell is seen. Type a JavaScript command at the prompt and press enter to see the results. This utility provides developers a way to test small amounts of code in a terminal. Also, JavaScript programmers are not restricted to web development.

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

jsc_0.png


"rhino" is just like jsc, but it provides more features and abilities. To execute a JavaScript file using rhino (via command-line), type "rhino -e FILEPATH". When typing "rhino" without parameters, a prompt will appear; this is the JavaScript interactive shell.

https://developer.mozilla.org/en-US/docs/Mozilla/Projects/Rhino
www.mozilla.org/rhino/shell.html

NOTE: jsc is similar to WebKit while rhino is related to Gecko.

JavaScript can also be transpiled. Transpilation is the process of converting one programming language to another. For instance, with the command-line utility "rhino-jsc", developers can convert JavaScript into Java class files (*.class). This further helps developers to use their JavaScript programming skills for uses not related to the web.

JavaScript can also be transpiled to and from a variety of languages. The below list is not a complete list of transpilers.

JS2Coffee (JS to Coffeescript) - http://js2coffee.org/
pyjs (Python to JS) - http://pyjs.org/
dart2js (Dart to JS) - https://www.dartlang.org/tools/dart2js/
GopherJS (Go to JS) - https://github.com/gopherjs/gopherjs

"Node.js" is a cross-platform runtime environment that runs on a server. Server admins can use Node.js on their server instead of HTML, HTTP, and web servers. Node.js processes JavaScript code and then sends the results/output to the client.

http://nodejs.org/

Once users are using a JavaScript shell, they could use npm to install "shelljs" (https://www.npmjs.org/package/shelljs) which provides portable Unix commands in JavaScript.

Node Package Manager (npm) is a lot like the “pip” command used by Python programmers to install more Python packages. npm is also similar to the “pear” command used by PHP developers for the same purpose. The general format for the npm command in a terminal is “npm install MODULE”.

https://www.npmjs.org/

Considering that JavaScript is a script that can run inside or outside a web-browser, many users may wonder about a hashpling. Well, JavaScript files do not require or typically use a hashpling/shebang. However, some systems may support “#!/usr/bin/env node”. Alternately, polyglot hashplings can be made. This requires the file itself being a shell script that initiates rhino or jsc to interpret the JavaScript code that is in the shell script. In addition, users could make a custom hashpling. For instance, in the first line of your JavaScript files, add this line “#!/usr/bin/env jsc” or “#!/usr/bin/env rhino”.

Various other JavaScript consoles and shell are available as seen in this list - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Shells

Further Reading
 

Attachments

  • slide.jpg
    slide.jpg
    42.8 KB · Views: 36,494
Last edited:



Staff online


Top