Skip to content

Instantly share code, notes, and snippets.

@fodra
Created November 15, 2017 05:03
Show Gist options
  • Save fodra/f010ae85304666eea102b154675ee432 to your computer and use it in GitHub Desktop.
Save fodra/f010ae85304666eea102b154675ee432 to your computer and use it in GitHub Desktop.
Using Function.prototype.apply() a practical example.

Issue

Being used to Python, I am looking for a min function in Javascript that would accept a list and returns the minimum value in the list.

Python

This is as easy as minimum = min([4, 4, 3, 2, 1])

Javascript

I only found Math.min() which accepts a comma delimited argument list as described in MDN.

To make this behave like Python's min function, here's when apply comes in handy.

let minimum = Math.min.apply(this, [4, 4, 3, 2, 1]);

@andrewartajos

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment