-1

In the Node js Documentation, I have seen something like this,

fs.readFile(path[, options], callback)

What do those square brackets mean? I'm sorry if this is duplicate, but I wasn't able to find an answer.

Phantom
  • 423
  • 1
  • 5
  • 13
  • That's documentation, not syntax. It's an Optional Argument. You have to have `path` and `callback`. If you put `options` in, it has to come after `path` and before `callback`. – StackSlave Jan 09 '20 at 01:27

1 Answers1

3

It means that the parameter is optional. Call like

fs.readFile('some/path', options, callback)

or

fs.readFile('some/path', callback)
Snow
  • 3,820
  • 3
  • 13
  • 39
  • so everything wrapped inside the square brackets are optional? – Phantom Jan 09 '20 at 01:28
  • Yes, that's it. – Snow Jan 09 '20 at 01:30
  • True, but don't include the square brackets! The square brackets are there just to inform you that certain arguments are optional. That example is uncommonly tricky because there is an optional argument sandwiched between two required arguments. Usually optional arguments are all at the end. So in this particular case, the method "sniffs" to see what type each argument is to determine if it is an object (for the options) or a function (for the callback). So if the second argument is a function, it will use that as the callback. – bob Jan 09 '20 at 03:11