I'd like to destructure an array inside a function.
The following example passes an array as a single argument, then attempts to destructure the argument inside the function. Seems simple enough, but node throws an error.
// volume.js
function volume(box) {
let [x,y,z] = box;
return x * y * z;
}
volume([2,3,4]);
Running that with $ node volume.js
(v5.1.1) returns this error:
./volume.js:2
let [x,y,z] = box;
^
ReferenceError: let is not defined
at volume (./volume.js:2:3)
at Object.<anonymous> (./volume.js:5:1)
at Module._compile (module.js:425:26)
at Object.Module._extensions..js (module.js:432:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:313:12)
at Function.Module.runMain (module.js:457:10)
at startup (node.js:138:18)
at node.js:974:3
There are other ways to solve this, but I don't understand why the above example doesn't work. Why does this throw an error? How can let
be undefined?