Reference
Operator Precedence
Operator precedence determines the order in which operations are evaluated. Higher precedence operators are evaluated first.
Precedence Table
Operators are listed from highest precedence (1) to lowest (11):
| Level | Operators | Description | Associativity |
|---|---|---|---|
| 1 | [] . | Member access, index | Left to right |
| 2 | () | Function call | Left to right |
| 3 | - ! na | Unary minus, logical NOT | Right to left |
| 4 | ** | Exponentiation | Right to left |
| 5 | * / % | Multiplication, division, modulo | Left to right |
| 6 | + - | Addition, subtraction | Left to right |
| 7 | < > <= >= | Comparison | Left to right |
| 8 | == != | Equality | Left to right |
| 9 | ebong | Logical AND | Left to right |
| 10 | ba | Logical OR | Left to right |
| 11 | = += -= *= /= | Assignment | Right to left |
Arithmetic Precedence
| 1 | // Multiplication before addition |
| 2 | dekho(2 + 3 * 4); // 14 (not 20) |
| 3 | // Equivalent to: 2 + (3 * 4) |
| 4 | |
| 5 | // Division before subtraction |
| 6 | dekho(10 - 6 / 2); // 7 (not 2) |
| 7 | // Equivalent to: 10 - (6 / 2) |
| 8 | |
| 9 | // Left to right for same precedence |
| 10 | dekho(20 / 4 * 2); // 10 |
| 11 | // Evaluated as: (20 / 4) * 2 |
| 12 | |
| 13 | // Exponentiation is right-associative |
| 14 | dekho(2 ** 3 ** 2); // 512 |
| 15 | // Evaluated as: 2 ** (3 ** 2) = 2 ** 9 |
Comparison vs Arithmetic
| 1 | // Arithmetic evaluated before comparison |
| 2 | dekho(5 + 3 > 6); // sotti |
| 3 | // Evaluated as: (5 + 3) > 6 = 8 > 6 |
| 4 | |
| 5 | dekho(10 - 2 == 4 * 2); // sotti |
| 6 | // Evaluated as: (10 - 2) == (4 * 2) = 8 == 8 |
| 7 | |
| 8 | dekho(2 * 3 <= 1 + 5); // sotti |
| 9 | // Evaluated as: (2 * 3) <= (1 + 5) = 6 <= 6 |
Logical Operators
| 1 | // Comparison before logical operators |
| 2 | dekho(5 > 3 ebong 2 < 4); // sotti |
| 3 | // Evaluated as: (5 > 3) ebong (2 < 4) |
| 4 | |
| 5 | // AND before OR |
| 6 | dekho(sotti ba mittha ebong mittha); // sotti |
| 7 | // Evaluated as: sotti ba (mittha ebong mittha) = sotti ba mittha = sotti |
| 8 | |
| 9 | // Use parentheses for clarity |
| 10 | dekho((sotti ba mittha) ebong mittha); // mittha |
| 11 | |
| 12 | // NOT has highest precedence among logical |
| 13 | dekho(na sotti ebong sotti); // mittha |
| 14 | // Evaluated as: (na sotti) ebong sotti = mittha ebong sotti |
Member Access
| 1 | dhoro obj = { |
| 2 | arr: [1, 2, 3], |
| 3 | fn: kaj(x) { ferao x * 2; } |
| 4 | }; |
| 5 | |
| 6 | // Member access has highest precedence |
| 7 | dekho(obj.arr[0]); // 1 |
| 8 | dekho(obj.fn(5)); // 10 |
| 9 | |
| 10 | // Chained access |
| 11 | dhoro nested = {a: {b: {c: 42}}}; |
| 12 | dekho(nested.a.b.c); // 42 |
Assignment
| 1 | // Assignment has lowest precedence |
| 2 | dhoro x = 5 + 3; // x = 8 |
| 3 | dhoro y = 2 * 4 > 6; // y = sotti |
| 4 | |
| 5 | // Assignment is right-associative |
| 6 | dhoro a = 1; |
| 7 | dhoro b = 2; |
| 8 | dhoro c = 3; |
| 9 | |
| 10 | a = b = c = 10; |
| 11 | dekho(a, b, c); // 10 10 10 |
| 12 | // Evaluated as: a = (b = (c = 10)) |
| 13 | |
| 14 | // Compound assignment |
| 15 | dhoro n = 10; |
| 16 | n += 5 * 2; // n = n + (5 * 2) = 10 + 10 = 20 |
Using Parentheses
Parentheses can override default precedence:
| 1 | // Without parentheses |
| 2 | dekho(2 + 3 * 4); // 14 |
| 3 | |
| 4 | // With parentheses |
| 5 | dekho((2 + 3) * 4); // 20 |
| 6 | |
| 7 | // Complex expression |
| 8 | dhoro result = (10 + 5) * (20 - 10) / (2 + 3); |
| 9 | dekho(result); // 30 |
| 10 | |
| 11 | // Logical expressions |
| 12 | dhoro a = sotti; |
| 13 | dhoro b = mittha; |
| 14 | dhoro c = sotti; |
| 15 | |
| 16 | dekho(a ba b ebong c); // sotti (a ba (b ebong c)) |
| 17 | dekho((a ba b) ebong c); // sotti ((a ba b) ebong c) |
Common Mistakes
| 1 | // Mistake 1: Assuming left-to-right for everything |
| 2 | dhoro x = 2 + 3 * 4; // x = 14, not 20 |
| 3 | |
| 4 | // Mistake 2: Forgetting AND has higher precedence than OR |
| 5 | dhoro result = sotti ba mittha ebong mittha; |
| 6 | // This is: sotti ba (mittha ebong mittha) = sotti |
| 7 | // Not: (sotti ba mittha) ebong mittha = mittha |
| 8 | |
| 9 | // Mistake 3: Comparison chaining doesn't work as expected |
| 10 | dhoro age = 25; |
| 11 | // This doesn't do what you think: |
| 12 | // 18 < age < 65 |
| 13 | |
| 14 | // Correct way: |
| 15 | jodi (age > 18 ebong age < 65) { |
| 16 | dekho("Working age"); |
| 17 | } |
| 18 | |
| 19 | // Mistake 4: Assignment in condition |
| 20 | // This assigns, doesn't compare: |
| 21 | // jodi (x = 5) { ... } |
| 22 | |
| 23 | // Correct: |
| 24 | jodi (x == 5) { dekho("Equal"); } |
Best Practices
- Use parentheses for clarity - Even when not strictly needed, they make intent clear
- Break complex expressions - Use intermediate variables for readability
- Be careful with logical operators - AND has higher precedence than OR
- Test edge cases - Verify your expressions work as expected
| 1 | // Good: Clear with parentheses |
| 2 | dhoro isValid = (age >= 18) ebong (hasPermission ba isAdmin); |
| 3 | |
| 4 | // Good: Broken into steps |
| 5 | dhoro basePrice = 100; |
| 6 | dhoro discount = 20; |
| 7 | dhoro tax = 10; |
| 8 | dhoro priceAfterDiscount = basePrice - discount; |
| 9 | dhoro finalPrice = priceAfterDiscount + (priceAfterDiscount * tax / 100); |
| 10 | |
| 11 | // Instead of: |
| 12 | // dhoro finalPrice = basePrice - discount + (basePrice - discount) * tax / 100; |