Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

11. Number() > Conceptual Overview of Using the Number() Object

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>

  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free 10-Day Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint