Let's suppose I wrote the following code snipped in VSC:
let a = [1,2,3,4,5];
console.log(Math.max(...a))
If you hover over max
, VSC gives you it's definition as:
(method) Math.max(...values: number[]): number
Returns the larger of a set of supplied numeric expressions.
@param values — Numeric expressions to be evaluated.
What does (...values: number[]): number
mean? Does that mean that it takes in an array and stores it as an array called number[]
internally? Also, what does @param values
mean?
CodePudding user response:
What does
(...values: number[]): number
mean?
This uses rest parameter syntax, and just means that all of the parameters are of the same type: number
. The return type of the function is also number
.
This syntax value: type
comes from TypeScript.
Does that mean that it takes in an array and stores it as an array called
number[]
internally?
If you were to implement Math.max
yourself using this signature, then yes, all of the arguments passed to the function would be stored in a variable called values
, which would be an array of numbers.
Also, what does
@param values
mean?
This is JSDoc syntax, and it refers to a function parameter.
CodePudding user response:
What does
(...values: number[]): number
mean?
It means it's a function that takes as many numbers as you want, each as separate parameters (ie, you can call Math.max(1, 2)
, or Math.max(1, 2, 3, 4, 5, 6)
, etc), and it returns a number
Does that mean that it takes in an array and stores it as an array called number[] internally?
No, it says nothing about the internal implementation, and no array needs to be involved. It's just telling you how you can use this function
Also, what does
@param values
mean?
To a human, it just means "this function takes a parameter called values
". Some automated tools look for the keyword @param
as part of JSDoc