JavaScript: Maths - numbers and operators
This page demonstrates how numbers can be manipulated using JavaScript.
JavaScript code snippets listed by task and organised in a tutorial style each building on the previous example. New topics are introduced when required.
1. |
Working with numbers: declare explicitly or implicitly. Create a Number object explicitly using the 'var' statement and 'new' keyword with the Number() constructor function: shown for completeness but we will not be creating our numbers this way. Better to create a number implicitly using 'var' and assignment of a literal value. |
2. |
Working with numbers: the arithmetic operators '+', '-', '*', '/', '%'. Introducing '++' and '--'. for increment and decrement on variables. Examples of calculation precedence.
JavaScript has a comprehensive range of operators for building expressions: JavaScript operators table. |
3. |
Experiment with using 'minus 0' technique on a string to convert to a number. Introducing 'typeof()' which returns the type of data that has been passed to it. Using 'isNaN()' on the data type. Examples of use of above with strings: '42.50', '42.50text' and 'text'. |
4. |
Working with the JavaScript built-in Math object. Math object is different: you do not create a Math object you just use it, the Math object does not store data. Math object contains a store of functions (methods). Some examples... |
|
See the Math object reference for list of essential methods and constants. |
|
5. |
Working with the JavaScript built-in Math object. Math object contains a store of constants (properties). Some examples... |
6. |
A simple use of the Math object. Working with 'random()' to mimic a dice throw. |
7. |
More complex code: but only incorporates much of what has been already covered in the tutorials. |
8. |
Build on the above and learn about the JavaScript Date object. View code examples on JavaScript: Dates page. |
| Suggest a code snippet to add to this category. | |
Back to top.
Copyright © 2006-2010 justfigures.co.uk

