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):

LevelOperatorsDescriptionAssociativity
1[] .Member access, indexLeft to right
2()Function callLeft to right
3- ! naUnary minus, logical NOTRight to left
4**ExponentiationRight to left
5* / %Multiplication, division, moduloLeft to right
6+ -Addition, subtractionLeft to right
7< > <= >=ComparisonLeft to right
8== !=EqualityLeft to right
9ebongLogical ANDLeft to right
10baLogical ORLeft to right
11= += -= *= /=AssignmentRight to left

Arithmetic Precedence

1// Multiplication before addition
2dekho(2 + 3 * 4); // 14 (not 20)
3// Equivalent to: 2 + (3 * 4)
4
5// Division before subtraction
6dekho(10 - 6 / 2); // 7 (not 2)
7// Equivalent to: 10 - (6 / 2)
8
9// Left to right for same precedence
10dekho(20 / 4 * 2); // 10
11// Evaluated as: (20 / 4) * 2
12
13// Exponentiation is right-associative
14dekho(2 ** 3 ** 2); // 512
15// Evaluated as: 2 ** (3 ** 2) = 2 ** 9

Comparison vs Arithmetic

1// Arithmetic evaluated before comparison
2dekho(5 + 3 > 6); // sotti
3// Evaluated as: (5 + 3) > 6 = 8 > 6
4
5dekho(10 - 2 == 4 * 2); // sotti
6// Evaluated as: (10 - 2) == (4 * 2) = 8 == 8
7
8dekho(2 * 3 <= 1 + 5); // sotti
9// Evaluated as: (2 * 3) <= (1 + 5) = 6 <= 6

Logical Operators

1// Comparison before logical operators
2dekho(5 > 3 ebong 2 < 4); // sotti
3// Evaluated as: (5 > 3) ebong (2 < 4)
4
5// AND before OR
6dekho(sotti ba mittha ebong mittha); // sotti
7// Evaluated as: sotti ba (mittha ebong mittha) = sotti ba mittha = sotti
8
9// Use parentheses for clarity
10dekho((sotti ba mittha) ebong mittha); // mittha
11
12// NOT has highest precedence among logical
13dekho(na sotti ebong sotti); // mittha
14// Evaluated as: (na sotti) ebong sotti = mittha ebong sotti

Member Access

1dhoro obj = {
2 arr: [1, 2, 3],
3 fn: kaj(x) { ferao x * 2; }
4};
5
6// Member access has highest precedence
7dekho(obj.arr[0]); // 1
8dekho(obj.fn(5)); // 10
9
10// Chained access
11dhoro nested = {a: {b: {c: 42}}};
12dekho(nested.a.b.c); // 42

Assignment

1// Assignment has lowest precedence
2dhoro x = 5 + 3; // x = 8
3dhoro y = 2 * 4 > 6; // y = sotti
4
5// Assignment is right-associative
6dhoro a = 1;
7dhoro b = 2;
8dhoro c = 3;
9
10a = b = c = 10;
11dekho(a, b, c); // 10 10 10
12// Evaluated as: a = (b = (c = 10))
13
14// Compound assignment
15dhoro n = 10;
16n += 5 * 2; // n = n + (5 * 2) = 10 + 10 = 20

Using Parentheses

Parentheses can override default precedence:

1// Without parentheses
2dekho(2 + 3 * 4); // 14
3
4// With parentheses
5dekho((2 + 3) * 4); // 20
6
7// Complex expression
8dhoro result = (10 + 5) * (20 - 10) / (2 + 3);
9dekho(result); // 30
10
11// Logical expressions
12dhoro a = sotti;
13dhoro b = mittha;
14dhoro c = sotti;
15
16dekho(a ba b ebong c); // sotti (a ba (b ebong c))
17dekho((a ba b) ebong c); // sotti ((a ba b) ebong c)

Common Mistakes

1// Mistake 1: Assuming left-to-right for everything
2dhoro x = 2 + 3 * 4; // x = 14, not 20
3
4// Mistake 2: Forgetting AND has higher precedence than OR
5dhoro 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
10dhoro age = 25;
11// This doesn't do what you think:
12// 18 < age < 65
13
14// Correct way:
15jodi (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:
24jodi (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
2dhoro isValid = (age >= 18) ebong (hasPermission ba isAdmin);
3
4// Good: Broken into steps
5dhoro basePrice = 100;
6dhoro discount = 20;
7dhoro tax = 10;
8dhoro priceAfterDiscount = basePrice - discount;
9dhoro finalPrice = priceAfterDiscount + (priceAfterDiscount * tax / 100);
10
11// Instead of:
12// dhoro finalPrice = basePrice - discount + (basePrice - discount) * tax / 100;