Browse - Programming Tips - How can I print from the command line js (javaScript) interpreter?

Date: 2016may11
Language: javaScript
OS: Linux
Keywords: display, output

Q.  How can I print from the command line js (javaScript) interpreter?

I can not use document.write() or alert() etc

A.  Use putstr() or print()

#!/usr/bin/js function sayHello() { putstr("hello\n"); } sayHello();
Its called SpiderMonkey. More info https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey/Introduction_to_the_JavaScript_shell To install on RedHat/Fedora/CentOS
dnf install js
You can also use it as a shell:
$ js > print(55);
There is a help() function:
echo 'help()' | js
Yields
Command Description ======= =========== version([number]) Get or force a script compilation version number revertVersion() Revert previously set version number options([option ...]) Get or toggle JavaScript options load(['foo.js' ...]) Load files named by string arguments evaluate(code) Evaluate code as though it were the contents of a file run('foo.js') Run the file named by the first argument, returning the number of of milliseconds spent compiling and executing it readline() Read a single line from stdin print([exp ...]) Evaluate and print expressions putstr([exp]) Evaluate and print expression without newline dateNow() Return the current time with sub-ms precision help([name ...]) Display usage and help messages quit() Quit the shell assertEq(actual, expected[, msg]) Throw if the first two arguments are not the same (both +0 or both -0, both NaN, or non-zero and ===) assertJit() Throw if the calling function failed to JIT gc() Run the garbage collector gcparam(name, value) Wrapper for JS_SetGCParameter. The name must be either 'maxBytes' or 'maxMallocBytes' and the value must be convertable to a positive uint32 countHeap([start[, kind]]) Count the number of live GC things in the heap or things reachable from start when it is given and is not null. kind is either 'all' (default) to count all things or one of 'object', 'double', 'string', 'function', 'qname', 'namespace', 'xml' to count only things of that kind makeFinalizeObserver() get a special object whose finalization increases the counter returned by the finalizeCount function finalizeCount() return the current value of the finalization counter that is incremented each time an object returned by the makeFinalizeObserver is finalized setDebug(debug) Set debug mode setDebuggerHandler(f) Set handler for debugger keyword to f setThrowHook(f) Set throw hook to f trap([fun, [pc,]] exp) Trap bytecode execution untrap(fun[, pc]) Remove a trap line2pc([fun,] line) Map line number to PC pc2line(fun[, pc]) Map PC to line number stackQuota([number]) Query/set script stack quota stringsAreUTF8() Check if strings are UTF-8 encoded testUTF8(mode) Perform UTF-8 tests (modes are 1 to 4) throwError() Throw an error from JS_ReportError build() Show build date and time clear([obj]) Clear properties of object intern(str) Internalize str in the atom table clone(fun[, scope]) Clone function object getpda(obj) Get the property descriptors for obj getslx(obj) Get script line extent toint32(n) Testing hook for JS_ValueToInt32 evalcx(s[, o]) Evaluate s in optional sandbox object o if (s == '' && !o) return new o with eager standard classes if (s == 'lazy' && !o) return new o with lazy standard classes if (s == 'split' && !o) return new split-object o with lazy standard classes evalInFrame(n,str,save) Evaluate 'str' in the nth up frame. If 'save' (default false), save the frame chain shapeOf(obj) Get the shape of obj (an implementation detail) resolver(src[, proto]) Create object with resolve hook that copies properties from src. If proto is omitted, use Object.prototype. sleep(dt) Sleep for dt seconds scatter(fns) Call functions concurrently (ignoring errors) snarf(filename) Read filename into returned string read(filename) Synonym for snarf compile(code) Compiles a string to bytecode, potentially throwing parse(code) Parses a string, potentially throwing timeout([seconds]) Get/Set the limit in seconds for the execution time for the current context. A negative value (default) means that the execution time is unlimited. elapsed() Execution time elapsed for the current context. parent(obj) Returns the parent of obj. wrap(obj) Wrap an object into a noop wrapper. serialize(sd) Serialize sd using JS_WriteStructuredClone. Returns a TypedArray. deserialize(a) Deserialize data generated by serialize. mjitstats() Return stats on mjit memory usage. stringstats() Return stats on string memory usage. newGlobal(kind) Return a new global object, in the current compartment if kind === 'same-compartment' or in a new compartment if kind === 'new-compartment'
Incomplete list of things I have noticed: Objects that do NOT exist: document window console FileReader Objects that DO exist: JSON