Javascript Operators [Link]
Operators
Maths
In [134… %%script node
[Link](3+5, 3-5, 3*5, 3/5, 5%3, 2**3) // plus, minus, multiply, divide, remainder, power
8 -2 15 0.6 2 8
In [135… %%script node
// unary -
const x = 3;
const y = -3;
[Link](-x, -y) // unary minus - returns new value with sign changed
-3 3
In [136… %%script node
// unary -
const x = "3";
const y = "-3";
[Link](-x, -y) // unary minus when used with strings,convert to number and changes sign, return that number
-3 3
In [137… %%script node
// unary +
const x = "3";
const y = "-3";
[Link](+x, +y) // unary plus when used with strings,convert to number, return that number
3 -3
In [138… %%script node
// pre and post increment
let x = 5;
const y = x++; // post increment -x will be incremented by 1 after assigning its value to y
1 of 5 23/09/24, 06:41
Javascript Operators [Link]
[Link](x, y)
const z = ++x; // pre increment - x will be incremented by 1 before assigning its value to z
[Link](x, z)
6 5
7 7
In [139… %%script node
// pre and post decrement
let x = 5;
const y = x--; // post decrement -x will be decremented by 1 after assigning its value to y
[Link](x, y)
const z = --x; // pre decrement - x will be decremented by 1 before assigning its value to z
[Link](x, z)
4 5
3 3
In [140… %%script node
// with + operator if one value is string and other is number, number get converted to string
[Link](3+"5")
// with other maths binary operators, if one value is string and other is number,string get converted to number
[Link](3 * "5")
35
15
Comparisons
In [141… %%script node
[Link](3 < 5, 3 > 5, 3 <= 5, 3 >= 5) // less than, greater than, less than equal to, greater than equal to
true false true false
In [142… %%script node
// Equality comparison with conversion
//if one value is string and other is number,string get converted to number
[Link]( 3 == 3, 3 == '3')
true true
2 of 5 23/09/24, 06:41
Javascript Operators [Link]
In [143… %%script node
// Equality comparison without conversion(strict equality comparison)
[Link]( 3 === 3, 3 === '3')
true false
In [144… %%script node
// Not equal comparison operator (!== compares without conversion)
[Link]( 3 != 3, 3 !== '3')
false true
Logical operators
In [145… %%script node
// a || b - if a is truthy then a else b, b will be evaluated only if a is falsy
[Link](0 || 9, true || false)
9 true
In [146… %%script node
// a && b - if a is falsy then a else b, b will be evaluated only if a is truthy
[Link](0 && 9, true && false)
0 false
In [147… %%script node
// ! a = if a is truthy then false, else true
[Link](!9)
false
Bitwise operators
In [148… %%script node
// bitwise and
[Link](0 & 0, 0 & 1, 1 & 0, 1 & 1)
0 0 0 1
3 of 5 23/09/24, 06:41
Javascript Operators [Link]
In [149… %%script node
// bitwise or
[Link](0 | 0, 0 | 1, 1 | 0, 1 | 1)
0 1 1 1
In [150… %%script node
// bitwise xor
[Link](0 ^ 0, 0 ^ 1, 1 ^ 0, 1 ^ 1)
0 1 1 0
In [151… %%script node
// bitwise left shift x << n equals to x * 2 to the power n
[Link](3 << 2)
12
In [152… %%script node
// bitwise right shift with sign - for possitive number x - x >>n equals to x floor divide 2 to the power n
[Link](12 >> 2)
Assignments
In [155… %%script node
let x, y, z;
x = 3 // assignment operator assigns value 3 to variable x.
y = z = 5 // will assign 5 to both y and z
[Link](x, y, z)
3 5 5
In [157… %%script node
// augmented assignment statements - works with math and bitwise operators
let x = 4, y = 2;
x += 5 // will add 5 to x
y &= 3 // can be written in place of y = y & 3;
[Link](x, y)
4 of 5 23/09/24, 06:41
Javascript Operators [Link]
9 2
Miscellaneous topics -Decimal to binary And binary to decimal conversion
In [153… %%script node
// decimal to binary
const x = 4;
[Link]([Link](2))
100
In [154… %%script node
// binary to decimal
const x = "100"
[Link](parseInt(x, 2))
5 of 5 23/09/24, 06:41