Built-in Objects
In addition to
console
there are other objects built into JavaScriptWe 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)
Other examples:
Last updated