Another way of perfoming a calculation is to use one of the
JavaScript built-in Math methods. These methods are applied
to an object called the Math object. The syntax for
applying a Math method is: |
value = Math.method(variable); |
where method is the method you'll apply to a variable, and
value is the resulting value. For example, to calculate the absolute
value of a variable named NumVar, you use the "abs" method as follows: |
AbsValue = Math.abs(NumVar); |
and the value of the AbsValue variable is set to the absolute value |
Math Method | Description |
---|---|
Math.abs(number) | Returns the absolute value of number |
Math.sin(number) | Calculates the sine of number, where number is an angle expressed in radians |
Math.cos(number) | Calculates the cosine of number, where number is an angle expressed in radians |
Math.round(number) | Rounds number to the closest integer |
Math.ceil(number) | Rounds number up to the next highest integer |
Math.floor(number) | Rounds number down to the next lowest integer |
Math.random(number) | Returns the year number |
Math.abs(number) | Returns a random number between 0 and 1 |
Operator | Description |
---|---|
= = | Returns true if variable are equal (x = y) |
!= | Returns true if variables are not equal (x != y) |
> | Returns true if the variable on the left is greater than the variable on the right (x > y) |
< | Returns true if the variable on the left is less than the variable on the right (x < y) |
>= | Returns true if the variable on the left is greater than or equal to the varible on the right (x >= y) |
>= | Returns true if the variable on the left is less than or equal to the varible on the right (x <= y) |
Operator | Description | Example |
---|---|---|
&& | Returns true only when both expressions are true | var x=20; var y = 25; |
|| | Returns true when either expression is true | (x = = 20)&&(y = = 25)returns true (x = = 20)&&(y = = 20)returns false (x = = 20)||(y = = 20)returns true (x = = 25)||(y = = 20)returns false |
| | Returns true if the expression is false, and false if the expression is true |
|(x == 20) returns false |(x == 20) returns true |
Method | Description | Example Date ("April, 8, 2002, 14:36:57") |
---|---|---|
getSeconds() | Returns the seconds | getSeconds() = 57 |
getMinutes() | Returns the minute | getMinutes() = 36 |
getHours() | Returns the hour in military time | getHours() = 14 |
getDate() | Returns the #day of the month | getDate() = 8 |
getDay() | Returns the day of the week | getDay() = 1(Monday) |
getMonth() | Returns the # of the month (0-11) | getMonth() = 3(April) |
getFullYear() | Returns the year number | getFullYear() = 2002 |
getTime() | Returns the time value, expressed in milliseconds | getTime() = 1,018,286,728,000 |