When it comes to JavaScript , there tends to be two schools of thought regarding IO (specifically printing).

Some golfers will use functions such as alert, document.write, and console.log (explicit printing), whereas other golfers will rely on the console implicitly logging the return value of statements executed in it (implicit printing).

I think it's worth discussing what the standard should be so that developers who make use of explicit printing are not at a disadvantage against developers using implicit printing. Of course, anyone posting a code golf could always explicitly override the standard.

Personally I believe that JavaScript code golf should work irrespective of where it's executed* (<script> element, browser console, node, etc), which means that explicit printing would be required.

* some golfs will require exceptions to this due to the nature of the different environments


3 Answers 3


Here is my proposal for a standard:

  • Use alert if the code runs in web browser (for example if it modifies DOM), or just uses core JavaScript (as it's easy to claim it's a browser code in this case). It should be used just once, as message boxes are annoying when shown multiple times (this means you should prepare a buffer for alert).

  • You can almost always use console.log. You need to if you work with non-browser environments, such as Node.js. For example, if code uses require in order to load stuff like Underscore.js, it has to use console.log. It's longer than alert however, so it probably won't be used outside of Node.js context.

  • Use print if you write code for SpiderMonkey, without web browser. I don't know why you would want to, but you can. Just tell that code requires SpiderMonkey, because web browsers use print for something else (printing on printer, that is).

  • Use document.write if you need to output HTML, or want to output in multiple statements, or something. Otherwise, alert is shorter.

  • Write a function when task asks you to, and return the value.

  • Never use implicit print statement. That's cheating in my opinion, and won't work outside of dev console. If you really want to, make Node.js code, and say it runs with -p. It also would waste 1 character (On "interactive" answers and other special conditions).

  • 1
    \$\begingroup\$ With node they can use -p just as with perl and ruby. Not using neither that +1 penalty is clear cheating. \$\endgroup\$
    – manatwork
    Commented Jan 4, 2014 at 12:50
  • \$\begingroup\$ @manatwork: Interesting, I didn't knew about this feature. Well, the standard for Perl golfing is that flag wastes the number of characters needed to write them. For example, -p is 3 characters (including a whitespace). Could be fine if you use Node.js, and you specify -p, I guess. \$\endgroup\$
    – null
    Commented Jan 4, 2014 at 18:40
  • \$\begingroup\$ According to On “interactive” answers and other special conditions, we count it 1. \$\endgroup\$
    – manatwork
    Commented Jan 4, 2014 at 18:53
  • \$\begingroup\$ @manatwork: Thanks for information. I find it interesting, but I guess this is fine. \$\endgroup\$
    – null
    Commented Jan 4, 2014 at 19:02
  • \$\begingroup\$ More specifically, we count it 1 if the script is eligible for a -e invocation in the presented form. Which is most often the case, yet not always. \$\endgroup\$
    – J B
    Commented Jan 5, 2014 at 1:47
  • \$\begingroup\$ console.log should be the only valid answer. With Node.js it is always stdout (by default) and relatively modern browsers support console messages. All relevant JavaScript engines use console.log. Aside: print also works in v8's d8. \$\endgroup\$
    – zamnuts
    Commented Oct 27, 2014 at 19:18
  • 2
    \$\begingroup\$ I completely disagree with the first point that people should limit the use of alert, because it is 'annoying'. This website is about golfing code, not in any way about UX. \$\endgroup\$ Commented Feb 4, 2016 at 12:12

I propose we adopt a standard I/O library with published specifications. This allows platforms with different I/O models to use variations of this standard library to just work. I have seen this approach used in textbooks with good results.


I have no idea how that's done in JavaScript, but for multi-language challenges, I don't think it should get any preferential treatment. The common case is “write to stdout”, and it should stick to that unless specifically allowed by the problem.

  • 7
    \$\begingroup\$ JavaScript doesn't typically have a standard out. \$\endgroup\$
    – Boann
    Commented Jan 9, 2014 at 3:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .