Chapter 6: Objects

6.4 Javascript Built-In Objects

Javascript Built-In ObjectsSo far, we’ve learned how to create our own objects in Javascript and add properties and methods to it.

Besides allowing us to create our own objects, Javascript also comes with a few useful built-in objects. These include Math, Date, Number, String etc. Let’s briefly discuss some of these objects and their properties and methods.

6.4.1 Math

First, let's start with the Math object. This object allows us to perform mathematical tasks on numbers. Some of its methods include:

Math.round(x)

Returns the value of x rounded to its nearest integer

Example:

Math.round(5.1) gives us 5.
Math.round(5.5) gives us 6.

Math.pow(x, y)

Returns the value of x to the power of y:

Example:

Math.pow(2, 5) gives us 32.

Math.min() and Math.max()

Math.min() and Math.max() returns the lowest and highest value in a list of arguments respectively.

Example:

Math.max(11, 20, 12, 15, 16) gives us 20.
Math.min(11, 20, 12, 15, 16) gives us 11.

Math.random()

Returns a random number between 0 (inclusive) and 1 (exclusive). We can multiply the result by a number x to give us a random number from 0 up to but not including x.

Example:

Math.random()*5 gives us a number between 0 and 5 (excluding 5)

We've covered some of the commonly used Math methods. For a full list of all the methods and properties available, check out
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math

6.4.2 Date

Next, let's look at the Date object. This object is used to work with dates and times. One of the most useful methods is the now() method. This method gives us the number of milliseconds since midnight Jan 1, 1970. We’ll be using this method in our project later.

Example:

Date.now() gives us 1509008917703 at the time of writing this sentence.

Besides the now() method, the Date object also comes with a lot of other methods that we can use. However to use most of these methods, we need to create a Date object first. There are four ways to do it:

var d = new Date();
var d = new Date(milliseconds);
var d = new Date(dateString);
var d = new Date(year, month, day, hours, minutes, seconds, milliseconds);

The first method creates a Date object that represents the current date and time.

The next three methods allow us to create Date objects that represent a specific date and time. For instance, we can create a Date object representing 2017 Nov 1, 10:15am.

To do that using the second method, we pass in the number of milliseconds between our date and midnight Jan 1, 1970.

To do that using the third method, we pass in a string that represents our date as shown below:

var d = new Date("November 1, 2017 10:15:00");

To do that using the fourth method, we pass in 7 arguments as shown below:

var d = new Date(2017, 11, 1, 10, 15, 00, 00);

The last 4 arguments are optional if we do not want to specify the time.

After creating the Date object, we can use it to call various methods. Some of these methods include

getDay() – Returns the day of the week (from 0-6, with 0 representing Sunday)
getDate() – Returns the day of the month (from 1-31)
getMonth() – Returns the month (from 0-11, with 0 representing January)
getFullYear() – Returns the year
getHours() – Returns the hour (from 0-23)
getMinutes() – Returns the minutes (from 0-59)
getSeconds() – Returns the seconds (from 0-59)
getMilliseconds() – Returns the milliseconds (from 0-999)

Example:

var d = new Date();

console.log(d.getDay());
console.log(d.getMonth());

We’ll get

4
9

as the output at the time of writing. This represents Thursday and Oct respectively.

For a full list of all the methods and properties of the Date object, check out
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date