1

I used

const args = process.argv; args.shift(); args.shift()

to line up my command line arguments and ignore the first 2 words node index.js

My code works just fine but it gives me a bit of anxiety regarding const as a word in NodeJS.

args is not a constant if i'm allowed to change it. Does anybody know why it works the way it does?

  • 1
    Does this answer your question? [Const in JavaScript: when to use it and is it necessary?](https://stackoverflow.com/questions/21237105/const-in-javascript-when-to-use-it-and-is-it-necessary) – joshwilsonvu Apr 24 '20 at 20:27

1 Answers1

2

const doesn't make something immutable, it just means you can't re-assign it to a new value. You can add whatever you want to existing object and array consts. For immutability, you would want a 3rd party library.

Regarding using args.shift();, I would caution against it since it mutates the args array in place. Why not do something like const myArgs = args.slice(2); which returns a copy of the array minus the first two items?

sdotson
  • 740
  • 3
  • 13