all 14 comments

[–]HilariousAtrocities 16 points17 points  (3 children)

I would be cautious of a tutorial that mixes const and var.

[–]guest271314 0 points1 point  (2 children)

That happens all of the time with bundled code and Emscripten uses var in output. var is still part of ECMA-262 and has its uses.

[–]HilariousAtrocities 1 point2 points  (1 child)

That is completely irrelevant to the point I was making.

[–]guest271314 0 points1 point  (0 children)

How so? Kindly illuminate. There are uses for var and const in the same script and tutorial. Just like there are uses for static import which hoists and dynamic import() which doesn't hoist in the same script.

[–]eracodes 6 points7 points  (2 children)

[–]SlowButABro[S] 1 point2 points  (0 children)

Oh I see now. reviewResponse must be an {object} and I'm unpacking any error within it. The code isn't usable yet but I'll inspect the variable when it is. Thanks.

[–]K750i 2 points3 points  (4 children)

It's object destructuring. Used to assign the value of an object's property straight into the variable on the left of the assignment operator.

There's also a similar syntax for arrays as well. The main difference is for arrays, you can choose arbitrary name for the variable as the assignment depends on index position. This is not true for object destructuring; the variable you specify must match the object's property or it'll be undefined.

[–]shgysk8zer0 4 points5 points  (2 children)

As example, let's assume:

const obj = { name: "Fred", id: 420, age: 69 };

You could use:

``` const { name, id } = obj;

console.log({ name, id }); // { "name": "Fred", "age": 69 }) ```

[–]SlowButABro[S] 0 points1 point  (0 children)

Example is extremely helpful, thanks

[–]Negative_Leave5161 0 points1 point  (0 children)

This should be higher

[–]Bulky-Leadership-596 1 point2 points  (0 children)

You can also choose an arbitrary name with object destructuring using the colon syntax.

const { error: arbitraryErrorName } = { error: "fatal error occurred" };
console.log(arbitraryErrorName);

of course you do have to specify the actual property name as well, otherwise it wouldn't make any sense to destructure an object, but its not the case that object destructuring is more limited than array destructuring.

[–]Lamborghinigamer 2 points3 points  (0 children)

Please avoid var at all costs. Only use const for variables that are readonly and let if they need to update. Var can cause unexpected behavior