Tuesday, April 24, 2007

JavaScript Operators

Arithmetic Operators




Assignment Operators




Comparison Operators




Logical Operators




String Operator

A string is most often text, for example "Hello World!". To stick two or more string variables together, use the + operator.

txt1="What a very"
txt2="nice day!"
txt3=txt1+txt2

The variable txt3 now contains "What a verynice day!".

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+" "+txt2ortxt1="What a very "
txt2="nice day!"
txt3=txt1+txt2

The variable txt3 now contains "What a very nice day!".


Conditional Operator

JavaScript also contains a conditional operator that assigns a value to a variable based on some condition.

Syntax
variablename=(condition)?value1:value2

Example
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.