GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialrepublic.com — JavaScript Numbers - Tutorial Republic JAVASCRIPT BASIC
JS Introduction
JS Getting Started
JS Syntax
JS Variables
JS Generating Output
JS Data Types
JS Operators
JS Events
JS Strings
JS Numbers
JS If…Else
JS Switch…Case
JS Arrays
JS Sorting Arrays
JS Loops
JS Functions
JS Objects
JAVASCRIPT & DOM
JS DOM Nodes
JS DOM Selectors
JS DOM Styling
JS DOM Get Set Attributes
JS DOM Manipulation
JS DOM Navigation
JAVASCRIPT & BOM
JAVASCRIPT ADVANCED
JS Date and Time
JS Math Operations
JS Type Conversions
JS Event Listeners
JS Event Propagation
JS Borrowing Methods
JS Hoisting Behavior
JS Closures
JS Strict Mode
JS JSON Parsing
JS Error Handling
JS Regular Expressions
JS Form Validation
JS Cookies
JS AJAX Requests
JS ES6 Features
JAVASCRIPT EXAMPLES
JAVASCRIPT REFERENCE
Advertisements
JavaScript NumbersIn this tutorial you will learn how to represent numbers in JavaScript. Working with NumbersJavaScript supports both integer and floating-point numbers that can be represented in decimal, hexadecimal or octal notation. Unlike other languages, JavaScript does not treat integer and floating-point numbers differently. All numbers in JavaScript are represented as floating-point numbers. Here's an example demonstrating the numbers in different formats: ExampleTry this code »
Extra large numbers can be represented in exponential notation e.g. 6.02e+23 (same as 6.02x1023). ExampleTry this code »
Tip: The biggest safe integer in JavaScript is Numbers can also be represented in hexadecimal notation (base 16). Hexadecimal numbers are prefixed with ExampleTry this code »
Note: Integers can be represented in decimal, hexadecimal, and octal notation. Floating-point numbers can be represented in decimal or exponential notation. Operating on Numbers and StringsAs you know from the previous chapters, the ExampleTry this code »
If you observe the above example carefully, you will find that the result of the last operation is not just a simple string concatenation, because operators with the same precedence are evaluated from left to right. That's why, since variables x and y both are numbers they are added first then the result is concatenated with the variable z which is a string, hence final result is But, if you perform other mathematical operations like multiplication, division, or subtraction the result will be different. JavaScript will automatically convert numeric strings (i.e. strings containing numeric values) to numbers in all numeric operations, as shown in the following example: ExampleTry this code »
Moreover, if you try to multiply or divide numbers with strings that are not numeric, it returns ExampleTry this code »
Representing InfinityInfinity represents a number too big for JavaScript to handle. JavaScript has special keyword ExampleTry this code »
Note: Infinity is a special value that represents the mathematical Infinity Avoiding Precision ProblemsSometimes, operations on floating-point numbers produce unexpected results, as shown here: ExampleTry this code »
As you can see the result is To avoid this problem you can use the solution something like this: ExampleTry this code »
JavaScript round floating-point numbers to 17 digits, which is enough precision or accuracy in most cases. Also, in JavaScript integers (numbers without fractional parts or exponential notation) are accurate is up to 15 digits, as demonstrated in the following example: ExampleTry this code »
Performing Operations on NumbersJavaScript provides several properties and methods to perform operations on number values. As you already know from the previous chapters, in JavaScript primitive data types can act like objects when you refer to them with the property access notation (i.e. dot notation). In the following sections, we will look at the number methods that are most commonly used. Parsing Integers from StringsThe If the Leading and trailing spaces are allowed. Here's an example: ExampleTry this code »
Note: The Similarly, you can use the ExampleTry this code »
Converting Numbers to StringsThe ExampleTry this code »
Formatting Numbers in Exponential NotationYou can use the ExampleTry this code »
Note: Exponential notation is useful for representing numbers that are either very large or very small in magnitude. For example, 62500000000 can be written as 625e+8 or 6.25e+10. Formatting Numbers to Fixed DecimalsYou can use the ExampleTry this code »
Formatting Numbers with PrecisionIf you want most appropriate form of a number, you can use the If precision is large enough to include all the digits of the integer part of number, then the number is formatted using fixed-point notation. Otherwise, the number is formatted using exponential notation. The precision parameter is optional. Here's an example: ExampleTry this code »
Finding the Largest and Smallest Possible NumbersThe Number object also has several properties associated with it. The A number that falls outside of the range of possible numbers is represented by a constant ExampleTry this code »
Also, check out the JavaScript math operations chapter to learn about rounding numbers, generating a random number, finding maximum or minimun value from a set of numbers, etc. Advertisements
Advertisements
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |