All Products
Search
Document Center

Operator

Last Updated: Feb 04, 2021

Arithmetic operator

 
  1. var a = 10, b = 20;
  2. // Addition operation
  3. console.log(30 === a + b);
  4. // Substraction operation
  5. console.log(-10 === a - b);
  6. // Multiplication operation
  7. console.log(200 === a * b);
  8. // Division operation
  9. console.log(0.5 === a / b);
  10. // Complementation operation
  11. console.log(10 === a % b);

The addition +</> operator can be used for string concatenation.

 
  1. var a = 'hello', b = ' alipay';
  2. // String concatenation
  3. console.log('hello alipay' === a + b);

Comparison operator

 
  1. var a = 10, b = 20;
  2. // Less than
  3. console.log(true === (a < b));
  4. // More than
  5. console.log(false === (a > b));
  6. // Less than or equal to
  7. console.log(true === (a <= b));
  8. // More than or equal to
  9. console.log(false === (a >= b));
  10. // Equal sign
  11. console.log(false === (a == b));
  12. // Not equal sign
  13. console.log(true === (a != b));
  14. // Equivalent sign
  15. console.log(false === (a === b));
  16. // Not equivalent sign
  17. console.log(true === (a !== b));

Binary logical operator

 
  1. var a = 10, b = 20;
  2. // AND
  3. console.log(20 === (a && b));
  4. // OR
  5. console.log(10 === (a || b));
  6. // NOT, negation operation
  7. console.log(false === !a);

Bitwise operator

 
  1. var a = 10, b = 20;
  2. // Left shift operation
  3. console.log(80 === (a << 3));
  4. // Unsigned right shift operation
  5. console.log(2 === (a >> 2));
  6. // Signed right shift operation
  7. console.log(2 === (a >>> 2));
  8. // AND operation
  9. console.log(2 === (a & 3));
  10. // XOR operation
  11. console.log(9 === (a ^ 3));
  12. // OR operation
  13. console.log(11 === (a | 3));

Assignment operator

 
  1. var a = 10;
  2. a = 10; a *= 10;
  3. console.log(100 === a);
  4. a = 10; a /= 5;
  5. console.log(2 === a);
  6. a = 10; a %= 7;
  7. console.log(3 === a);
  8. a = 10; a += 5;
  9. console.log(15 === a);
  10. a = 10; a -= 11;
  11. console.log(-1 === a);
  12. a = 10; a <<= 10;
  13. console.log(10240 === a);
  14. a = 10; a >>= 2;
  15. console.log(2 === a);
  16. a = 10; a >>>= 2;
  17. console.log(2 === a);
  18. a = 10; a &= 3;
  19. console.log(2 === a);
  20. a = 10; a ^= 3;
  21. console.log(9 === a);
  22. a = 10; a |= 3;
  23. console.log(11 === a);

Unary operator

 
  1. var a = 10, b = 20;
  2. // Auto increment operation
  3. console.log(10 === a++);
  4. console.log(12 === ++a);
  5. // Auto decrement operation
  6. console.log(12 === a--);
  7. console.log(10 === --a);
  8. // Positive value operation
  9. console.log(10 === +a);
  10. // Negative value operation
  11. console.log(0-10 === -a);
  12. // Not operation
  13. console.log(-11 === ~a);
  14. // Negation operation
  15. console.log(false === !a);
  16. // Delete operation
  17. console.log(true === delete a.fake);
  18. // Void operation
  19. console.log(undefined === void a);
  20. // Typeof operation
  21. console.log("number" === typeof a);

Ternary operator

 
  1. var a = 10, b = 20;
  2. // Conditional operator
  3. console.log(20 === (a >= 10 ? a + 10 : b + 10));

Comma operator

 
  1. var a = 10, b = 20;
  2. // Comma operator
  3. console.log(20 === (a, b));

Operator precedence

SJS operators have the same precedence as Javascript operators.