Monday , 20 November 2017
Home / Programming / JavaScript / Number Object – JavaScript Tutorial
Number Object
Number Object

Number Object – JavaScript Tutorial

This tutorial is part of the JavaScript tutorial series for beginners. To view the whole list of tutorials available and the order in which you should follow them, then click here.

So we’ve talked about objects in general and we discovered a new object called the document object. JavaScript comes with built in objects that you can use. In this tutorial, we take a look at the number object.

Number Object Properties

Just like other objects, you can access the properties and methods of the number object. Let’s take a look at how to do this.

You should be familiar with document.write() by now. We’ll be using this often instead of the alert box to show messages from now on. What’s important is what’s inside this method.

In order to use the number object, you first have to type Number. It’s important to note that JavaScript is case sensitive. So, you must type the N in Number with a capital letter or else it will not work.

We then access it’s properties by using dot notation  followed by the name of the property we want to access. As you can see, we’re doing this 3 times and we’re accessing 3 different values. The MAX_VALUE property will return the largest number possible in JavaScript. The MIN_VALUE returns the lowest number possible in JavaScript. The POSITIVE_INFINITY returns infinity.

Another thing to know is that we’re adding + ‘<br’> after each number. The plus sign can be used to append values together. We’re appending a break line in HTML to better format the output. If you did everything right, you should see your page with the following output.

1.7976931348623157e+308
5e-324
Infinity

You’ll notice that both the maximum value and the minimum value are just decimals. This is because the numbers are so large and small that JavaScript had to convert it into a exponential notation. Hence, you should see an e followed by a + or – to indicate in which direction the value should be written followed by the exponential value.

To be honest, you won’t be using the number object’s properties. You will most likely be using it’s methods. Let’s take a look at a few of it’s methods.

Number Object Methods

Clear your script and add this bit of code to your script.

You’ll notice that I’m not using the Number keyword to access the number object. This is because whenever you create a variable with a value of a number, then it also inherits the properties and methods of the number object. JavaScript will automatically do this for you. You also need to do this in order to use methods in the number object. If you tried accessing these methods directly, then you would get an error because there’s no number to apply the methods to begin with.

The toExponential() method will convert a number into exponential notation. The toString() method will convert a number into a string. If you refresh your page you should get something like this.

5.50343e+2
550.343

Conclusion

It’s important to note that I didn’t go over every property and method of the number object. The reason is because this object has A LOT of properties and methods. Actually, most built in objects have a lot of properties and methods. Some of them are useful and some of them are not. In this tutorial series, I will only be going over the properties and methods of an object that I believe are necessary to know.  Trying to memorize all the properties and methods of an object  is useless if you aren’t ever going to use them in the first place.

If you would like more information about the number object then go here.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number

About Jasko Koyn

Check Also

Try-Catch Statement

Try-catch Statement – Advanced JavaScript Tutorial

The try-catch statement is a unique way to handle errors. When errors occur, usually it …

Leave a Reply

Your email address will not be published. Required fields are marked *

ULTIMATE WORDPRESS DEVELOPMENT COURSE
Want to learn how to create your own custom WordPress plugins and themes? Check out my course!
Get 93% off my course. You'll also learn how to integrate WooCommerce and BuddyPress into your projects. This is a limited time offer
Yes, I would like to get 93% off your course!
No Thanks!
close-link