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


Comments

Popular posts from this blog

Steve Lopez and the Importance of Newspapers

Beaucoup de gens la trouvent très belle

Ideas for fixing unconnected computing