How do I output something in Rhino?
Asked Answered
U

2

16

I'm looking for the javascript equivalent of Python2.x's print "hi". I'm working with the Rhino javascript interpreter in the ubuntu terminal. When I type:

document.write{"hi"}

I get the error that 'document' is not defined.

Urumchi answered 4/4, 2012 at 10:49 Comment(2)
From the CLI is it not just print('hi');Martijn
Advice: use node instead of rhino (it has for starters a better implemented prompt). You can just run a js file like this: node myfile.js. If you want to print in a node script just use console.log like in the browser.Urumchi
H
27

JavaScript doesn't have any built in methods to provide output. Scripts have to depend on features provided by the host environment for that.

document is an object that is available in web browsers, but not in Rhino. Even if it was available, document.write is a function. You use () to call a function, not {}.

Rhino provides a print function.

print("hi");
Hooker answered 4/4, 2012 at 10:53 Comment(0)
S
2

I don't think you have access to the 'document' object - as the one I think you're referring to is only available when javascript is run in the browser.

Also, use normal brackets rather than curly brackets to invoke functions.

Just try:

print('Hello, world!')
Stemma answered 4/4, 2012 at 10:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.