// This method finds the absolute value of a number. > Math.abs(-3); 3
All standard, built-in JS objects can be found listed at this resource:
As the name suggests these "Global Objects" can be called on from anywhere in our JS or DevTools console. We want to highlight two of these objects:
Math object has a bunch of useful functionality related to Math:
Okay, well maybe these aren't so useful if you are not doing mathematical calculations — that's okay. It's still good to know these exist.
Date object also has useful functionality. Things like:
Date object also may not be helpful if you are not creating functionality around dates, but it is good to be aware of its existence. There's a small learning curve to working with the
Date object, since it uses numbers to represent each date, and these numbers are the milliseconds elapsed since 1 January 1970 UTC. There's a reason for this system, and you'll get to explore it when the time comes!
What's important now is to explain the difference between static and instance methods so that you are more prepared to explore both of these objects.
So far when we've talked about string or number methods, we've technically been talking about instance methods. An instance method is a method that is called on an instance of an object type. The same is true for instance properties. For example, we can create an instance of the
String object like so:
> const wifiPassCode = "iLovePizza2000";
wifiPassCode is actually an instance of the
String object type. We can call instance methods on it like:
> wifiPassCode.toUpperCase(); "ILOVEPIZZA2000"
Strings also have one instance property that we can access on every single instance of a string:
> wifiPassCode.length; 14
String instance property
length returns the number of characters in the string.
So what is a static property or method? A static property or method is one that is called on the object type itself. For example, an instance of a
Number and an instance method looks like this:
> let howManyPaintingsIveMade = 32; > howManyPaintingsIveMade.toString(); "32"
Number static method and property looks like this:
> Number.MAX_SAFE_INTEGER; 9007199254740991 > Number.isNaN(0/0); true
As we can see, static properties and methods are called on the object type itself. These object types are always capitalized. If we don't capitalize them, we'll get
undefined or an error returned:
> number.MAX_SAFE_INTEGER; undefined > number.isNaN(0/0); Uncaught TypeError: number.isNan is not a function at <anonymous>:1:8
The easiest way to distinguish between an instance method and a static one is to look at the method definition on MDN. If a method is listed with
prototype, it is an instance method. For example,
String.prototype.toUpperCase() Number.prototype.toString() Array.prototype.concat()
However, there's no easy way to distinguish between instance and static properties. Sometimes static properties are all uppercased, but that's not consistent across all JS objects. The best thing to do is just reference the MDN page for an object you are working with (or want to work with). Each reference page distinguishes between static and instance when it comes to listing properties and methods. Also note that JS object types can have any number of instance or static properties and methods. For example, the
Math object consists solely of static properties and methods.