• Identifiers must start with a letter, underscore (_), or dollar sign ($) - ( $ is common in PHP); subsequent characters can also be digits (0-9). Do NOT begin a variable name with a number!
• Examples of usable variable names include: Number_total, $name, _temp1. I highly recommend using naming conventions to make your code easier to read and easier to remember what you were trying to accomplish.
• Assignment Operators - assign a value to its left operand based on the value of its right operand. The basic assignment operator is equal (=), which assigns the value of its right operand to its left operand. That is, x = y assigns the value of y to x.
• Shorthand for assignment operators:
x += y same as x = x + y
x -= y same as x = x - y
x *= y same as x = x * y
x /= y same as x = x / y
x %= y same as x = x % y
• Comparison Operators - compares its operands and returns a logical value based on whether the comparison is true. The operands can be numerical, string, logical, or object values.
3 == var1
"3" == var1
3 == '3'
Not equal (!=)
var1 != 4
var2 != "3"
Greater than (>)
Returns true if the left operand is greater than the right operand.
var2 > var1
Greater than or equal (>=)
Returns true if the left operand is greater than or equal to the right operand.
var2 >= var1
var1 >= 3
Less than (<)
Less than or equal (<=)
Returns true if the left operand is less than the right operand.
var1 < var2
Returns true if the left operand is less than or equal to the right operand.
var1 <= var2
var2 <= 5
1/2 (returns 0 in Java)
• Other Arithmetic Operators:
(Modulus) - Returns the integer remainder of dividing the two operands.
Ex: 12 % 5 returns 2.
(Increment) - Adds one to its operand. If used as a prefix operator (++x), returns the value of its operand after adding one; if used as a postfix operator (x++), returns the value of its operand before adding one.
Ex: If x is 3, then ++x sets x to 4 and returns 4, whereas x++ sets x to 4 and returns 3.
(Decrement) - Subtracts one to its operand. The return value is analogous to that for the increment operator.
Ex: If x is 3, then --x sets x to 2 and returns 2, whereas x++ sets x to 2 and returns 3.
• Logical Operators - typically used with Boolean (logical) values; when they are, they return a Boolean value. However, the && and || operators actually return the value of one of the specified operands, so if these operators are used with non-Boolean values, they may return a non-Boolean value.
&& (expr1 && expr2)
(Logical AND) Returns expr1 if it can be converted to false; otherwise, returns expr2. Thus, when used with Boolean values, && returns true if both operands are true; otherwise, returns false.
|| (expr1 || expr2)
(Logical OR) Returns expr1 if it can be converted to true; otherwise, returns expr2. Thus, when used with Boolean values, || returns true if either operand is true; if both are false, returns false.
(Logical NOT) Returns false if its single operand can be converted to true; otherwise, returns true.
• Examples of expressions that can be converted to false are those that evaluate to null, 0, the empty string (""), or undefined.
The following code shows examples of the && (logical AND) operator:
a1=true && true // t && t returns true
a2=true && false // t && f returns false
a3=false && true // f && t returns false
a4=false && (3 == 4) // f && f returns false
a5="Cat" && "Dog" // t && t returns Dog
a6=false && "Cat" // f && t returns false
a7="Cat" && false // t && f returns false
The following code shows examples of the || (logical OR) operator:
o1=true || true // t || t returns true
o2=false || true // f || t returns true
o3=true || false // t || f returns true
o4=false || (3 == 4) // f || f returns false
o5="Cat" || "Dog" // t || t returns Cat
o6=false || "Cat" // f || t returns Cat
o7="Cat" || false // t || f returns Cat
The following code shows examples of the ! (logical NOT) operator:
n1=!true // !t returns false
n2=!false // !f returns true
n3=!"Cat" // !t returns false
Short-Circuit Evaluation - as logical expressions are evaluated left to right, they are tested for possible "short-circuit" evaluation using the following rules:
• false && anything is short-circuit evaluated to false.
• true || anything is short-circuit evaluated to true.
The rules of logic guarantee that these evaluations are always correct. Note that the anything part of the above expressions is not evaluated, so any side effects of doing so do not take effect.
• String Operators - in addition to the comparison operators, which can be used on string values, the concatenation operator (+) concatenates two string values together, returning another string that is the union of the two operand strings.
For example, "my " + "string" returns the string "my string".
The shorthand assignment operator += can also be used to concatenate strings. For example, if the variable mystring has the value "alpha," then the expression mystring += "bet" evaluates to "alphabet" and assigns this value to mystring.