Are Javascript built-in objects like Math always available without having to import them?
We used to call them, special objects, the group of, Math
, Date
, RegExp
, and so on. They were none of them part of the daily lexicon but drawn in by some next level need. They are there on demand with no need to import. They’re right there, built in, albeit, lazy loaded.
The only concern we would have is memory usage which is to say, rather far up the road from this perspective. Right now we don’t care about memory. That will come later.
Since we don’t care about memory, right now, we can load the Math module just by accessing any one of its methods. Going a step further, we can alias that method so that it looks like a function…
rand = Math.random
That one line just loaded the entire Math module into the namespace and captured a reference to the .random()
method that is now also an identifier in the shared namespace.
console.log(rand())
0.3415209583226535
Just as there are provisos that go along with the Math module, we can expect them in turn for the remaining modules we may find occasional need of. The Date
and RegExp
modules are cool to have, but have a lot of overhead so can impact performance, overall.
JavaScript is fast, but not as fast as Python because at its lowest level it is still not byte code. It cannot be pre-compiled, but for a few patterns and is therefore memory intensive during runtime. More memory means less speed. Pointing is still a counting operation for the computer.
Anyway, I’ve already said we’re not concerned about memory or any of that malarkey so delve into those objects and learn one a week on the side. Skip the big dreams: Just play with them so you get a feel. Spend all the time you need going over their documentation and write lots of demos on each scenario so you really get it ingrained.
Make it totally on the side, some time dedicated to it, some time dedicated to lessons and the related reading, practice, review, &c… You get the picture. It’s a weekly side project. See if you don’t come away a few months from now with everyone eating out of your hand, so to speak.
Truth be told, I almost answered this as a Python question. Whew, good thing I looked up.
>>> import builtins
>>> [*filter(lambda x: x[0].islower(), dir(builtins))]
The above will give us a list of all the built-in functions of the Python standard library (version 3.8). There may be a way for us to access the same thing in JS, but expect it to be messy in vanilla. The window object has a lot of properties. The ones we use regularly are buried in there somewhere.