Built-in Objects

  • In addition to console there are other objects built into JavaScript

  • We can build our own objects.

  • JavaScript has a built in Math object.

  • Objects have methods which we can use to perform certain actions on data types.

Example: console.log(Math.random());

  • This prints a random number between 0 (inclusive) and 1 (exclusive).

  • In the above example we called a method .random() which we appended with the object name with the dot operator.

  • To generate a random number between 0 and 50 we could multipy the result with 50, like so: Math.random() * 50; This can result in a decimal value, to make the result a whole number we can use this following method: Math.floor(); Math.floor(Math.random() * 50)

    console.log(Math.floor(Math.random() * 50)); // Prints a random whole number between 0 and 50

Other examples:

console.log(Math.random() * 100)
// 4.631185550006789
  

console.log(Math.floor(Math.random() * 100))
// 60
  

console.log(Math.ceil(43.8))
// 44
  

console.log(Number.isInteger(2017))
// true

Last updated