if else statement, switch statement, operators
ITWeb/개발일반 2008. 2. 20. 16:12•JavaScript If…Else Statements
–if ( CONDITION ) {
–}
–if ( CONDITION ) {
–} else {
–}
–if ( CONDITION ) {
–} else if ( CONDITION ) {
–}
–if ( CONDITION ) {
–} else if ( CONDITION ) {
–} else {
–}
•JavaScript Switch Statement
–switch(n) {
– case 1:
– execute code block 1
– break;
– case 2:
– execute code block 2
– break;
– default:
– code to be executed if n is
– different from case 1 and 2
–}
JavaScript Operators
Operator |
Description |
Example |
Result |
+ |
Addition |
x=2 y=2 x+y |
4 |
- |
Subtraction |
x=5 y=2 x-y |
3 |
* |
Multiplication |
x=5 y=4 x*y |
20 |
/ |
Division |
15/5 5/2 |
3 2.5 |
% |
Modulus (division remainder) |
5%2 10%8 10%2 |
1 2 0 |
++ |
Increment |
x=5 x++ |
x=6 |
-- |
Decrement |
x=5 x-- |
x=4 |
Operator |
Example |
Is The Same As |
= |
x=y |
x=y |
+= |
x+=y |
x=x+y |
-= |
x-=y |
x=x-y |
*= |
x*=y |
x=x*y |
/= |
x/=y |
x=x/y |
%= |
x%=y |
x=x%y |
Operator |
Description |
Example |
== |
is equal to |
5==8 returns false |
=== |
is equal to (checks for both value and type) |
x=5 x==y returns truey="5" x===y returns false |
!= |
is not equal |
5!=8 returns true |
> |
is greater than |
5>8 returns false |
< |
is less than |
5<8 returns true |
>= |
is greater than or equal to |
5>=8 returns false |
<= |
is less than or equal to |
5<=8 returns true |
–-&&
§ If first operand is truthy
– then result is second operand
– else result is first operand
–-||
§ If first operand is truthy
– then result is first operand
– else result is second operand
Operator |
Description |
Example |
&& |
and |
x=6 (x < 10 && y > 1) returns truey=3 |
|| |
or |
x=6 (x==5 || y==5) returns falsey=3 |
! |
not |
x=6
y=3 !(x==y) returns true |
-String
§txt1="What a very";
§txt2="nice day!";
§txt3=txt1+txt2;
-Conditional
§variablename = (condition) ? value1 : value2;
-alert
§var result = alert("sometext");
§alert(result); // undefined
-confirm
§var result = confirm("sometext");
§alert(result); // 확인:true, 취소:false
-prompt
§var result = prompt("sometext", "defaultvalue");
§alert(result); // 확인:string, 취소:null