Frequently used built-in global objects in Javascript

This tutorial is a part of the Learn everything about Javascript in one course.

In this tutorial, we will quickly go through frequently used global objects and fuctions in Javascript. The purpose is to let know that Javascript offers these functionalitlies out of the box.

At this point, you should be comfortable reading documentation (from MDN for example).

Math object, tool to assist with mathematics and numbers

Math object, tool to assist with mathematics and numbers. Some notable tools are:

  • Constant: Euler's Math.E, PI Math.PI, ...
  • Calculation: absolute Math.abs(x), cosine Math.cos(x), round Math.round(x), get a random number Math.random() ...
  • ... more on MDN.

JavaScript Date objects represent a single moment in time in a platform-independent format. Date objects contain a Number that represents milliseconds since 1 January 1970 UTC:

  • Get numeric value corresponding to the current time Date.now().
  • Get the day of the month (1–31) Date.getDate().
  • Get year (4 digits for 4-digit years) Date.getFullYear().
  • ... more on MDN.

setInterval(), run a function every specified period of time

Just like setTimeout(), but the only difference is the passed in function is executed every milisecond interval. We can clear this function execution by using clearInterval(). clearInterval() is also a global function.

setInterval.js
// setInterval(), run a function every specified period of time

 // sayHelloIntervalId is the Id used to clear the interval
const sayHelloIntervalId = setInterval(() => {
  console.log('say hello every 1000 miliseconds')
}, 1000) // 1000 miliseconds = 1 second

setTimeout(() => {
  clearInterval(sayHelloIntervalId) // clear the periodically called function
  console.log('will not say hello anymore!')
}, 3500); // 3500 miliseconds = 3.5 seconds

// say hello every 1000 miliseconds
// say hello every 1000 miliseconds
// say hello every 1000 miliseconds
// will not say hello anymore!

Full list of global objects and functions in Javascript

It's good to know the full list of global objects and functions Javascript offers. Why? Because they are frequently used. It saves time and money. You don't have to build them from scatch. But don't go ahead and learn all of them. At this point, knowing they exist and what they are for is good enough.

Summary

  1. Math, Date and setInterval are global and used frequently in Javascript.
  2. It's good to know the existence of full list of global objects and functions in Javascript and what they offer to assist.