Skip to content

Operator Types

Unary

OperatorOperationOperand TypePositionResult Type
~Bitwise NegationIntprefixInt
!Logical NegationBoolprefixBool
-Arithmetic NegationFloat/IntprefixFloat/Int
++IncrementFloat/Intprefix and postfixFloat/Int
--DecrementFloat/Intprefix and postfixFloat/Int

The increment and decrement values function a bit differently depends if they are used as a prefix or a postfix operator, being that when used as a prefix, the incremented/decremented value is returned, but when used as a postfix, the original value is returned

haxe
var a = 1; // a is 1
var b = a++; // b is 1, a is 2
var c = ++a; // c is 3, a is 3

Binary

Arithmetic operators

OperatorOperationOperand 1Operand 2Result type
%ModuloFloat/IntFloat/IntFloat/Int
*MultiplicationFloat/IntFloat/IntFloat/Int
/DivisionFloat/IntFloat/IntFloat
+AdditionFloat/IntFloat/IntFloat/Int
-SubtractionFloat/IntFloat/IntFloat/Int

Generally speaking if any of the operands are floats, then the result will also be a float.

haxe
var a = 1 + 2; // adds them
var b = 2 * a; // multiplication with variables
var c = b / 2; // division
var d = a - b; // subtraction
var e = 20 % 9 // modulo, gives the reainder after division

String concatenation operator

OperatorOperationOperand 1Operand 2Result type
+concatenationanyStringString
+concatenationStringanyString
+=concatenationStringanyString

When performing any concatenation/addition involving strings, other values would be automatically be converted to a string.

Logical Operators

OperatorOperationOperand 1Operand 2Result type
&&logical andBoolBoolBool
``logical orBool

Short circuting

Logical Operators are evaluated from left to right, but only as far as necessary. For example

haxe
// If the right side is false, then the left is never evaluated
// This is because only one of them has to be false for the entire condition to fail
var a = foo != null && foo.count == 1

Compound assignment operators

OperatorOperationOperand 1Operand 2Result type
%=moduloFloat/IntFloat/IntFloat/Int
*=multiplicationFloat/IntFloat/IntFloat/Int
/=divisionFloatFloat/IntFloat
+=additionFloat/IntFloat/IntFloat/Int
-=subtractionFloat/IntFloat/IntFloat/Int
haxe
var a = 5;
a += 4; // same as a = a + 4

Numeric comparison operators

OperatorOperationOperand 1Operand 2Result type
==equalFloat/IntFloat/IntBool
!=not equalFloat/IntFloat/IntBool
<less thanFloat/IntFloat/IntBool
<=less than or equalFloat/IntFloat/IntBool
>;greater thanFloat/IntFloat/IntBool
>;=greater than or equalFloat/IntFloat/IntBool

String comparison operators

OperatorOperationOperand 1Operand 2Result type
==equalStringStringBool
!=not equalStringStringBool
<lexicographically beforeStringStringBool
<=lexicographically before or equalStringStringBool
>lexicographically afterStringStringBool
>=lexicographically after or equalStringStringBool
haxe
var a = "foo";
var b = "bar";
var c = "foo";
var x = (a == b); // false
var y = (a == c); // true
var z = (a == "foo"); // true

Ternary Operator

OperatorOperationOperand 1Operand 2Operand 3Result type
?:conditionBoolanyanyany

Operand 2 and 3 must be the same type.

The ternary conditional operator is a shorter form of if:

haxe
// Tenary Operators are also expressions

var b = 1 == 2 ? 3 : 4; // 4

// equivalent to:
var b = if (1 == 2) 3 else 4; // 4