To make a new number, a simple initialization suffices:
var foo = 0; // or whatever number you want
foo = 1; //foo = 1 foo += 2; //foo = 3 (the two gets added on) foo -= 2; //foo = 1 (the two gets removed)
Number literals define the number value. In particular:
Unlike strings, arrays, and dates, the numbers aren't objects, so they don't contain any methods that can be accessed by the normal dot notation. Instead a certain Math object provides usual numeric functions and constants as methods and properties. The methods and properties of the Math object are referenced using the dot operator in the usual way, for example:
var varOne = Math.ceil(8.5); var varPi = Math.PI; var sqrt3 = Math.sqrt(3);
Returns the least integer greater than the number passed as an argument.
var myInt = Math.ceil(90.8); document.write(myInt); //91;
Returns the greatest integer less than the number passed as an argument.
var myInt = Math.floor(90.8); document.write(myInt); //90;
Returns the highest number from the two numbers passed as arguments.
var myInt = Math.max(8, 9); document.write(myInt); //9
Returns the lowest number from the two numbers passed as arguments.
var myInt = Math.min(8, 9); document.write(myInt); //8
Generates a pseudo-random number.
var myInt = Math.random;
Returns the closest integer to the number passed as an argument.
var myInt = Math.round(90.8); document.write(myInt); //91;
Properties of the Math object are most commonly used constants or functions:
Manage research, learning and skills at IT1me. Create an account using LinkedIn to manage and organize your IT knowledge. IT1me works like a shopping cart for information -- helping you to save, discuss and share.