DEV Community

Cover image for Make Math Global
Zevan Rosser
Zevan Rosser

Posted on

Make Math Global

Object.getOwnPropertyNames(Math).forEach(i => window[i] = Math[i]);

// or with map, just to be shorter
Object.getOwnPropertyNames(Math).map(i => window[i] = Math[i]);

// if this points to window
Object.getOwnPropertyNames(Math).map(i => this[i] = Math[i]);

// or using the deprecated "with" statement
with (Math) {
  console.log(PI, E, SQRT2, cos(1));
Enter fullscreen mode Exit fullscreen mode

While not very useful, I sometimes like to make the entire Math object global on the window – just when speed coding and playing around.

See more stuff like this over @ Snippet Zone

Discussion (0)