Hostingheaderbarlogoj
Join InMotion Hosting for $3.49/mo & get a year on Tuts+ FREE (worth $180). Start today.
Advertisement

Number()

by
Gift

Want a free year on Tuts+ (worth $180)? Start an InMotion Hosting plan for $3.49/mo.

This post is part of a series called JavaScript Succinctly.
String()
Boolean()

The Number() constructor function is used to create numeric objects and numeric primitive values.

In the following sample, I detail the creation of numeric values in JavaScript.

Sample: sample49.html

<!DOCTYPE html><html lang="en"><body><script>

	// Create number object using the new keyword and the Number() constructor.
	var numberObject = new Number(1);
	console.log(numberObject); // Logs 1.
	console.log(typeof numberObject) // Logs 'object'.

	// Create number literal/primitive using the number constructor without new.
	var numberObjectWithOutNew = Number(1); // Without using new keyword.
	console.log(numberObjectWithOutNew); // Logs 1.
	console.log(typeof numberObjectWithOutNew) // Logs 'number'.

	// Create number literal/primitive (constructor leveraged behind the scenes).
	var numberLiteral = 1;
	console.log(numberLiteral); // Logs 1.
	console.log(typeof numberLiteral); // Logs 'number'.

</script></body></html>

Integers and Floating-Point Numbers

Numbers in JavaScript are typically written as either integer values or floating-point values. In the following code, I create a primitive integer number and a primitive floating-point number. This is the most common usage of number values in JavaScript.

Sample: sample50.html

<!DOCTYPE html><html lang="en"><body><script>

	var integer = 1232134;
	console.log(integer); // Logs '1232134'.

	var floatingPoint = 2.132;
	console.log(floatingPoint); // Logs '2.132'.

</script></body></html>

A numeric value can be a hexadecimal value or octal value in JavaScript, but this is typically not done.


Number() Parameters

The Number() constructor function takes one parameter: the numeric value being created. In the following snippet, we create a number object for the value 456 called numberOne.

Sample: sample51.html

<!DOCTYPE html><html lang="en"><body><script>

	var numberOne = new Number(456);

	console.log(numberOne); // Logs '456{}'.

</script></body></html>

When used with the new keyword, instances from the Number() constructor produce a complex object. You should avoid creating number values using the Number() constructor (use literal/primitive numbers) due to the potential problems associated with the typeof operator. The typeof operator reports number objects as 'object' instead of the primitive label ('number') you might expect. The literal/primitive value is just more concise.


Number() Properties

The Number() object has the following properties:

Properties (e.g., Number.prototype;)


Number Object Instance Properties and Methods

Number object instances have the following properties and methods (not including inherited properties and methods):

Instance Properties (e.g., var myNumber = 5; myNumber.constructor;)

Instance Methods (e.g., var myNumber = 1.00324; myNumber.toFixed();)


Conclusion

The Number() constructor will surely be useful to you in your JavaScript adventures.

Advertisement