2Minutes: Built-in Static Objects In JavaScript
Most of us are familiar with constructor objects such as Array, Boolean, and Number, as they are core pillars of almost any JavaScript project.
However, many developers know very little about JavaScript’s built-in static objects. Starting with this article, we’ll tackle them one by one, beginning with the Math object.
Math
Math is a built-in static namespace object. As its name suggests, it is primarily used for mathematical operations, providing properties and methods that cover most common use cases.
Rather than listing every single method, this article focuses on how it behaves under the hood. Take this circle area calculation, for example:
function getCircleArea(radius) {
return Math.PI * radius ** 2;
}
// Math.PI has { writable: false }, so assigning to it fails or throws in strict mode
// It should not be reassigned:
Math.PI = 3.13;
console.log(Math.PI); // We will always get 3.141592653589793 when strict mode is off
One down! In the next one, we’ll dive into the JSON static object.
That’s 2Minutes, see you in the next one !
Sources
MDN Manual: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math
GeeksForGeeks: https://www.geeksforgeeks.org/javascript/javascript-math-object/