1

Please, if anyone out there can, explain to me or tell me what the line of code const [anyVariable] really means here:

router.put('/:id', withAuth, async (req, res) => {
try {
    const [anyVariable] = await BlogPost.update(req.body, {
        where: {
        id: req.params.id,
        },
});

Thank you! --Willie

Willie
  • 103
  • 1
  • 10
  • 2
    this is array destructuring assignment in JS. Suppose you have array `[1, 2]`. `const [a] = [1, 2];` will assign `variable a` with `value 1`. – Rahul Kumar May 20 '21 at 05:05
  • 2
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment – Rahul Kumar May 20 '21 at 05:05

1 Answers1

0

It's called array destructuring assignment in JS. It unpack values from arrays, or properties from objects, into individual variables.

const [num1, num2] = [11, 22, 33];
console.log(num1, num2);
Rahul Kumar
  • 3,009
  • 2
  • 16
  • 22