Scripting: Operations

From Waltz
Jump to navigation Jump to search

This documentation is partially reproduced from the excellent Mozilla Developer Network Reference for JavaScript.

Operations in JavaScript are used between objects to perform comparisons or arithmetic on those values.

Comparison Operators

Comparison operators are operators that computes a new Boolean by comparing two values. The result of the operation will always be either true or false. A comparison operator 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. Strings are compared based on standard lexicographical ordering, using Unicode values. In most cases, if the two operands are not of the same type, JavaScript attempts to convert them to an appropriate type for the comparison. This behavior generally results in comparing the operands numerically. The sole exceptions to type conversion within comparisons involve the === and !== operators, which perform strict equality and inequality comparisons. These operators do not attempt to convert the operands to compatible types before checking equality.

Operator Description Examples Returning True
Equal (==) Returns true if the operands are equal. 3 == 3

3 == "3"

0 == false

1 == true

"dog == 'dog'

Strict Equal (===) Returns true if the operands are equal and of the same type. 3 === 3

"dog" === 'dog'

Not Equal (!=) Returns true if the operands are not equal. 3 != 4

3 != "4"

2 != false

2 != true

"dog != 'cat'

Strict Not Equal (!==) Returns true if the operands are of the same type but not equal. 3 !== 4

3 !== "3"

0 !== false

1 !== true

"dog" !== 'cat'

Greater Than (>) Returns true if the left operand is greater than the right operand. 3 > 2

3 > "2"

Less Than (<) Returns true if the right operand is greater than the left operand. 2 < 3

"2" < 3

Greater Than or Equal (>=) Returns true if the left operand is greater than or equal to the left operand. 3 >= 2

3 >= 3

3 >= "2"

2 >= "3"

Less Than or Equal (<=) Returns true if the right operand is greater than or equal to the left operand. 2 <= 3

2 <= 2

"2" <= 3

"2" <= 2

Arithmetic Operations

An arithmetic operator takes numerical values (either literals or variables) as their operands and returns a single numerical value. The standard arithmetic operators are addition (+), subtraction (-), multiplication (*), and division (/). These operators work as they do in most other programming languages when used with floating point numbers (in particular, note that division by zero produces Infinity).

In addition to the standard arithmetic operations (+, -, * /), JavaScript provides the arithmetic operators listed in the following table:

Operator Description Example
Remainder (%) Binary operator. Returns the integer remainder of dividing the two operands. 12 % 5 returns 2.
Increment (++) Unary operator. 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. If x is 3, then ++x sets x to 4 and returns 4, whereas x++ returns 3 and, only then, sets x to 4.
Decrement (--) Unary operator. Subtracts one from its operand. The return value is analogous to that for the increment operator. If x is 3, then --x sets x to 2 and returns 2, whereas x-- returns 3 and, only then, sets x to 2.
Unary negation (-) Unary operator. Returns the negation of its operand. If x is 3, then -x returns -3.
Unary plus (+) Unary operator. Attempts to convert the operand to a number, if it is not already. +"3" returns 3.

+true returns 1.

Logical Operators

Logical operators are 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. The logical operators are described in the following table.

Operator Usage Description
Logical AND (&&) expr1 && expr2 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.
Logical OR (||) expr1 || expr2 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 (!) !expr Returns false if its single operand that can be converted to true; otherwise, returns true.

Examples of expressions that can be converted to false are those that evaluate to null, 0, NaN, the empty string (""), or undefined.

The following code shows examples of the && (logical AND) operator.

var a1 =  true && true;     // t && t returns true

var a2 =  true && false;    // t && f returns false

var a3 = false && true;     // f && t returns false

var a4 = false && (3 == 4); // f && f returns false

var a5 = 'Cat' && 'Dog';    // t && t returns Dog

var a6 = false && 'Cat';    // f && t returns false

var a7 = 'Cat' && false;    // t && f returns false

The following code shows examples of the || (logical OR) operator.

var o1 =  true || true;     // t || t returns true

var o2 = false || true;     // f || t returns true

var o3 =  true || false;    // t || f returns true

var o4 = false || (3 == 4); // f || f returns false

var o5 = 'Cat' || 'Dog';    // t || t returns Cat

var o6 = false || 'Cat';    // f || t returns Cat

var o7 = 'Cat' || false;    // t || f returns Cat

The following code shows examples of the ! (logical NOT) operator.

var n1 = !true;  // !t returns false

var n2 = !false; // !f returns true

var n3 = !'Cat'; // !t returns false

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:

print('my ' + 'string'); // console logs the string "my string".

The shorthand assignment operator += can also be used to concatenate strings.

For example:

var mystring = 'alpha';
mystring += 'bet'; // evaluates to "alphabet" and assigns this value to mystring.
Expressions and Scripting
Concepts Script Expressions · JavaScript
Fundamentals Constructors · Functions · Grammar · Operations · Outputs · Parameters · Referencing Nodes
Advanced Topics Flow Control · Consumer Functions · Loops and Iterations · Reference Chains
Errors Recursion · Script Exceptions · Syntax Errors · Try and Catch