nth root of a number in javaScript

Often I end up using Math.sqrt in projects whenever I need to get the square root of a number. However what if I want the nth root of a number? Such as the cubes root of a number, or any other degree beyond of that of just the number 2.

1 - nth root and math pow

One solution is to use the Math.pow method to get the nth root of a given root number and degree.

This works by just passing the root number as the base for Math.pow, and then diving 1 by the degree number to get the exponent argument for Math.pow like so.

1
2
3
4
5
6
var root = 25,
degree = 3;
var x = Math.pow(root, 1 / degree); // 5
console.log(Math.sqrt(num)); // 5

2 - Conclusion

I can not say that I end up using this often in projects, but it is something that I think about now and then. If I find out some other ways to go about doing that as well as some actual use case example i will be sure to update this post.