DEV Community

Cover image for Make Math Global
Zevan Rosser
Zevan Rosser

Posted on

1 1

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

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay