Chapter 11. Number()

Conceptual Overview of Using the Number() Object

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

In the code below, I detail the creation of numeric values in JavaScript.

Live Code

<!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 scene)
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 code below, I create a primitive integer number and a primitive floating point number. This is the most common usage of number values in JavaScript.

Live Code

<!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>

Note

In JavaScript, a numeric value can be a hexadecimal ...

Get JavaScript Enlightenment now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.