Data Type | Examples |
Number | 1 1024 18.0 3.14159 1.024e3 |
String | "" "foo" "2 words" |
Boolean | true false |
Operator | Name | Data Types | Examples | Result |
= | Assignment | Number String Boolean |
x = 3; y = 7.654; Str = "foo"; flag = true; |
x == 3 y == 7.654 Str == "foo" flag == true |
+ | Addition or Concatenation |
Number String |
x = 4 + 3; y = x + 7.432; Str = "foo" + "bar"; |
x == 7 y == 14.432 Str == "foobar" |
- | Subtraction | Number | x = 7 - 2; y = x - 3.359; |
w == 5 y == 1.641 |
* | Multiplication | Number | x = 2 * 5; y = x * 9.773; |
x == 10 y == 97.73 |
/ | Division | Number | x = 10 / 4; y = x / 8; |
x == 2.5 y == .3125 |
% | Remainder1 | Number | x = 10 % 4; y = x % .75; |
x == 2 y == .5 |
&& | AND | Boolean | flag = true; D = true && flag; D = D && false; |
flag == true D == true D == false |
|| | OR | Boolean | flag = true; D = flag || true; D = false || D; |
flag == true D == true D == false |
! | NOT | Boolean | D = !true; D = !D; |
D == false D == true |
1. The Remainder operator is often referred to as the Modulo operator. |
Operator | Name | Data Types | Examples |
== | Equality | Number String Boolean |
3 == 3 "test" == "test" false == false |
!= | Inequality | Number String Boolean |
12 != 39 "foo" != "bar" false != true |
< | Less Than | Number String1 |
2 < 3.1415 "a" < "b" |
<= | Less Than or Equal | Number String1 |
17 <= 17 "ABC" <= "ABD" |
> | Greater Than | Number String1 |
12 > -4 "foo" > "bar" |
>= | Greater Than or Equal | Number String1 |
3.99 >= 3.98 "fool" >= "foo" |
1. Strings follow standard dictionary ordering,
i.e., "A" < "B" < ... < "Z" < "a" < "b" ... < "z" "a" < "aa" < ... < "ab" < "aba" < ... < "b" |
Statement Name | General Form | Examples |
Comment | /* Comment... */ |
/* A comment that goes onto more than one line */ |
If-Then Statement | } else { else-clause }
|
if (x >= 0) { x = x + 3; } // decrement x if neg., // otherwise increment if (x < 0) { x = x - 1; } else { x = x + 1; } |
While Loop | }
|
sum = 0; while (x >= 1) { sum = sum + x; x = x - 1; } |
For Loop | }
|
sum = 0; for (i = 1; i <= x; i = i + 1) { sum = sum + i; } // fact = 1*2*3*...*10 fact = 1; for (j = 2; j <= 10; j = j + 1) { fact = fact * j; } |
Function | Examples | Result |
Square Root | x = Math.sqrt(9); | x == 3 |
Absolute Value | x = Math.abs(-23); y = Math.abs(99); |
x == 23 y == 99 |
Maximum | x = Math.max(3,14); | x == 14 |
Minimum | x = Math.min(3,14); | x == 3 |
Power | x = Math.pow(2,3); | x == 8 |
Floor | x = Math.floor(3.14159); y = Math.floor(4.5623); |
x == 3 y == 4 |
Ceiling | x = Math.ceil(3.14159); y = Math.ceil(4.5623); |
x == 4 y == 5 |
Round | x = Math.round(3.14159); y = Math.round(4.5623); |
x == 3 y == 5 |
ParseFloat | x = parseFloat("12.8"); y = parseFloat("ABC"); |
x == 12.8 y == NaN |
Prompt | theName = prompt("Enter Your
Name:","Default Value"); Netscape will display a dialog box, and theName will be assigned the value that is entered in the text field. |
|
Write | document.write("Howdy"); document.write(2+3); |
displays "Howdy" displays 5 |
Action | General Form | Examples |
Function Definition | variable-declarations function-body return return-value; }
|
{ return "foo"; } function BarFunc(N) // Assumes: N >= 0 // Returns: string of N "bar"s { var i, theStr; theStr = ""; for (i = 1; i <= N; i = i + 1) { theStr = theStr+"bar"; } return theStr; } function Greet(name) // Assumes: name is a string // Results: displays greeting { document.write("Hi "+name); } |
Function Call |
|
// Result: Str1 == "foo" Str2 = BarFunc(3); // Result: Str2 == "barbarbar" Greet("Dave"); // Result: displays "Hi Dave" |