w3pop.com :: ÍøÂçѧԺ :: JavaScript :: JS ²Ù×÷·û
| 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 y="5" x==y returns true |
| != | 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 |
| Operator | Description | Example |
|---|---|---|
| && | and Óë |
x=6 y=3 (x < 10 && y > 1) returns true |
| || | or »ò |
x=6 y=3 (x==5 || y==5) returns false |
| ! | not ·Ç |
x=6 y=3 !(x==y) returns true |
A string is most often text, for example "Hello World!". To stick two or more string variables together, use the + operator.
ÔÚÎÄ×Öµ±ÖÐʹÓõıȽ϶࣬¾ÙÀýÀ´Ëµ“Hello World!”Òª½«Á½¸ö»ò¶à¸ö×Ö·û´®±äÁ¿ÏνÓÔÚÒ»ÆðµÄ»°¾ÍµÃʹÓà + ·ûºÅ
txt1="What a very" txt2="nice day!" txt3=txt1+txt2 |
The variable txt3 now contains "What a verynice day!".
txt3±äÁ¿ÏÖÔÚ°üº¬“What a verynice day!”£¨°Ñ1ºÍ2ÏÎ½ÓÆðÀ´ÁË£©
To add a space between two string variables, insert a space into the expression, OR in one of the strings.
Òª¸øÁ½¸ö×Ö·û´®±äÁ¿ÖмäÌí¼Ó¿Õ¸ñ¾ÍµÃÔÚ±í´ïʽÀï²åÈë¿Õ¸ñ£¬»òÔÚÆäÖеÄÒ»¸ö¼ÓÉÏ£¨¿Õ¸ñ£©
txt1="What a very" txt2="nice day!" txt3=txt1+" "+txt2 or txt1="What a very " txt2="nice day!" txt3=txt1+txt2 |
The variable txt3 now contains "What a very nice day!".
ÏÖÔÚ±äÁ¿txt3Ϊ“What a very nice day!”
JavaScript also contains a conditional operator that assigns a value to a variable based on some condition.
JSÓиù¾ÝÌõ¼þ²»Í¬¸ø±äÁ¿²»Í¬ÖµµÄÌõ¼þÔËËã·û
variablename=(condition)?value1:value2 |
greeting=(visitor=="PRES")?"Dear President ":"Dear " |
If the variable visitor is equal to PRES, then put the string "Dear President " in the variable named greeting. If the variable visitor is not equal to PRES, then put the string "Dear " into the variable named greeting.
Èç¹û±äÁ¿visitorµÄÖµµÈÓÚPRESÄÇôgreetingµÄÖµ¾ÍΪ"Dear President "¡£Èç¹û²»ÎªPRESÄÇôgreetingµÄÖµ¾ÍΪ"Dear"