05. Global Objects

We have already seen a few global data types, such as Numbers, Strings, and Booleans. Let's look at the Math, Date, and Random objects, which can be incredibly useful.

Math

The Math objects provides us with the tools necessary to easily perform mathematical calculations.

PI
3.1415926...
round(n)
Round n to the nearest whole number.
sqrt(n)
Calculate squareroot of n.
ceil(n)
Find the nearest whole number greater than a number n.
floor(n)
Find the nearest whole number less than a number n.
> Math.ceil(Math.PI)
4
> Math.sqrt(Math.PI)
1.7724538509055159 

Calculating a Random value

The Math.random() method calculates a random number between 0 and 1. Thus, to get a number between 1 and 10, we can multiple our decimal value by 10 and use the floor() method to get a whole integer value, then add 1.

> Math.random()
0.5574872007127851
> Math.floor((Math.random() * 10) + 1)
7

Date

The Date object allows us to find and manipulate the date. This includes finding the days of the week, time, current year, etc.

Instantiating a new Date object

When working with Dates, you must instantiate a Date object with the new keyword. This new Date object will hold the current date and time.

There are several ways you can instantiate a Date object. Note that some values have a zero-based index (such as month), while others don't (like date).

> var date = new Date(2009, 9, 13, 3, 34, 44)
> date.toDateString()
"Tue Oct 13 2009"
> var date = new Date(2019, 9, 13)
> date.toDateString()
"Tue Oct 13 2019"
> var date = new Date('Mar 23, 1992 19:23:42')
> date.toDateString()
"Mon Mar 23 1992" 
Year
4 digit integer
Month
0-11
Day
1-31
Hour
0-23
Minutes
0-59
Seconds
0-59
Milliseconds
0-999

Date methods

get/setDate()
Return or set the day of the month.
getDay()
Return day of the week (integer from 0 to 6).
get/setFullYear()
Return or set the year (4-digit integer).
get/setHours()
Get or set the hours (0 to 23).
get/setMilliseconds()
Get or set milliseconds (0 to 999).
get/setMinutes()
Get or set the number of minutes (0 to 59).
get/setMonths()
Get or set the month (0 to 11).
get/setTime()
Get or set the time. Number of milliseconds since Jan. 1 1970 (aka Unix time).
getTimezoneOffset()
Returns timezone offset in minutes.
toDateString()
Returns date as a string. eg. Wed Mar 15 2003
toTimeString()
Returns time as a string.
toString()
Returns string of specified date.

Aching back from coding all day?

Foam Seat Cushion

Aching back from coding all day? Try Back Problems

This foam seat cushion relieves lowerback pain, numbness and pressure sores by promoting healthy weight distribution, posture and spine alignment. Furthermore, it reduces pressure on the tailbone and hip bones while sitting. Perfect for sitting on the computer desk for long periods of time.

$ Check price
99.9599.95Amazon 4.5 logo(9,445+ reviews)

More Back Problems resources

Build modern and responsive webpages

JavaScript & JQuery

Build modern and responsive webpages Try Front-end

This book was written for anyone who wants to use JavaScript to make their websites a little more interesting, engaging, interactive, or usable. In particular, it is aimed at people who do not have a degree in computer science (well, not yet anyway).

$ Check price
39.9939.99Amazon 4.5 logo(385+ reviews)

More Front-end resources

Ad