Numeric Value Literals

This section provides descriptions on numeric value literals and a tutorial example on how to use them in JavaScript source code.

To provide values of numbers in JavaScript source code, you need to use numeric value literals in 3 formats:

1. Integer Literal - A sequence of digits following an optional sign character to represent an integer value between -9007199254740992 (-2**53) and 9007199254740992 (2**53) inclusive. For example: 2008, -12, etc.

2. Hexadecimal Literal - A sequence of hexadecimal digits following 0x or 0X representing a positive integer or floating point value. A hexadecimal literal may also have a sign character. For example: 0x07D8 (2008), -0x0C (-12), 0xffffffffffffffffffff (1.2089258196146292e+24), etc.

3. Floating Point Literal - The scientific notation of a real number with the following syntax: (sign)(digits).(digits)e(sign)(digits). For example: 4.13, 0.333333, -1.51e10, 4.10e-29, etc.

In JavaScript, all numbers, including integers, are stored in 64-bit floating point format as described in the IEEE 754 standard.

Here is a JavaScript tutorial example that shows you how to use different types of number literals in JavaScript source code:

<html>
<!-- Number_Literals.html
   Copyright (c) 2013 by Dr. Herong Yang, herongyang.com
-->
<head><title>Number Literals</title></head>
<body>
<pre>
<script type="text/javascript">
   document.write(2008);
   document.write('\n');

   document.write(0x07D8);
   document.write('\n');

   document.write(-0x0C);
   document.write('\n');

   document.write(0xffffffffffffffffffff);
   document.write('\n');

   document.write(-1.51e10);
   document.write('\n');

   document.write(4.10e-29);
   document.write('\n');
</script>
</pre>
</body>
</html>

The output of this sample JavaScript is:

2008
2008
-12
1.2089258196146292e+24
-15100000000
4.1e-29

Table of Contents

 About This Book

 Introduction to JavaScript

 ECMAScript Language Specification and JavaScript Dialects

Data Types, Variables and Expressions

 Primitive Data Types - Numbers, Strings, and Booleans

Numeric Value Literals

 String Literials

 Declaring Variables - "var" Statements

 Operators and Expressions

 Operators and Expressions - Examples

 Flow Control Statements

 Creating, Accessing, and Manipulating Arrays

 Defining and Calling Functions

 Web Browser Supporting JavaScript

 Server-Side and Client-Side Web Scripting

 Introduction to Objects

 Defining Your Own Object Types

 Inheritance of Properties and Methods through the Prototype Object Chain

 'jrunscript' - JavaScript Shell Command from JDK

 Using Functions as "Function" Objects

 Introduction to Built-in Object Types

 W3C's Document Object Model (DOM) Specifications

 References

 PDF Printing Version