REPL: Read-Eval-Print Loop

From version 1.5 PhantomJS will provide an Interactive Mode, also known as REPL: Read-Eval-Print Loop.

Why a REPL?

Sometimes you feel the urge to test out some small/medium size Javascript code to see how x or y works. Usually you would open the Javascript console in Firebug, the Webkit Inspector, Opera Dragonfly or Chrome DevTools and type away. The addition of a REPL now allows you to do the same from PhantomJS.

How does it work

As mentioned above, REPL stands for READ-EVAL-PRINT LOOP: every line you type (READ) (i.e. type then press ⏎) is sent to the interpreter for immediate execution (EVAL) and feedback (PRINT).

This is (from version 1.5 onward) the new default mode in which PhantomJS starts if you don’t provide any parameter.

Here is an example use that will probably explain better than many words:

Every command gets evaluated on the spot, and the actual return value is printed.

To exit? The usual: either type phantom.exit() or CTRL+C or CTRL+D.

The entire API is available to use in this mode, so you can follow the examples and enter them line by line in REPL and they will work.

Auto-completion

One of the feature that we saw could bring a lot of value is auto-completion. Sometimes, as much as people like documentation, if they could get a list of methods or properties by just tapping TAB (i.e. →|) it could make all the difference: