diff --git a/doc/api/vm.md b/doc/api/vm.md index a62923c2a1a317..8c49cfb7c31f24 100644 --- a/doc/api/vm.md +++ b/doc/api/vm.md @@ -212,8 +212,24 @@ that sandbox][contextified] so that it can be used in calls to [`vm.runInContext()`][] or [`script.runInContext()`][]. Inside such scripts, the `sandbox` object will be the global object, retaining all of its existing properties but also having the built-in objects and functions any standard -[global object][] has. Outside of scripts run by the vm module, `sandbox` will -remain unchanged. +[global object][] has. Outside of scripts run by the vm module, global variables +will remain unchanged. + +```js +const util = require('util'); +const vm = require('vm'); + +var globalVar = 3; + +const sandbox = { globalVar: 1 }; +vm.createContext(sandbox); + +vm.runInContext('globalVar *= 2;', sandbox); + +console.log(util.inspect(sandbox)); // 2 + +console.log(util.inspect(globalVar)); // 3 +``` If `sandbox` is omitted (or passed explicitly as `undefined`), a new, empty [contextified][] sandbox object will be returned.