Free email newsletter: “ES.next News

2013-12-20

Using strict mode in the Node.js REPL

If you want to use strict mode [1] in the Node.js REPL, you have two options.

Option 1. Start the REPL as usual, wrap your code in an IIFE [2]:

    > (function () { 'use strict'; 'abc'.length = 1 }());
    TypeError: Cannot assign to read only property 'length'
(Assigning to a read-only property fails silently in sloppy mode.)

Option 2. Use the Node.js command line option --use_strict:

    node --use_strict
Afterwards, everything you write in the REPL is interpreted in strict mode:
    > 'abc'.length = 1
    TypeError: Cannot assign to read only property 'length'

References:

  1. JavaScript’s strict mode: a summary
  2. JavaScript variable scoping and its pitfalls

No comments: