0

I came accross this line of javascript code

let {max}=Math;

that allows you to do this:

let a=max(1,2) //2

Without using the Math object like this:

Math.max(1,2)

I don't know where to find the documentation of this syntax because all I have is that line of code and not a single clue to help me search on google so anyone knows about this practice ?

mondersky
  • 441
  • 2
  • 15

2 Answers2

4

This is called Destructuring Assignment and you can find documentation here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

Garrett Motzner
  • 3,021
  • 1
  • 13
  • 30
2

What you see there is called Destructuring assignment. You'll find plety of examples when you Google for Destructuring assignment JS ;)

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

Anyway, in this particular case you assign the property max (a function in this case) from Class Math. This allows you to just call max directly.

Imagine having an object DestructTest with a property x Then you can get the property x by just typing const {x} = DestructTest Instead of const propX = DestructTest.x JavaScript takes care for you to get the wanted property for you if you just ask for the right name ;) Hope this makes things a bit more clear. Cheers

LetItBeAndre
  • 151
  • 1
  • 10