15

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.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
Bentley4
  • 10,678
  • 25
  • 83
  • 134
  • From the CLI is it not just print('hi'); – Alex K. Apr 04 '12 at 10:54
  • 1
    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. – Bentley4 Mar 30 '14 at 13:58

2 Answers2

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");
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
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!')
zaf
  • 22,776
  • 12
  • 65
  • 95