13

I thought that Math.pow(2,2) was equal to 2^2 but it is not. So what does ^ (caret) mean in JavaScript?

I was executing some tests in the console but didn't recognize the results:

2 ^ 2 = 0
2 ^ 3 = 1
1 ^ 2 = 3
Aurora0001
  • 13,139
  • 5
  • 50
  • 53
Simon Edström
  • 6,461
  • 7
  • 32
  • 52

5 Answers5

14

It means bitwise XOR.

EDIT: Fixed link

Alexander Pavlov
  • 31,598
  • 5
  • 67
  • 93
3

It's a bitwise integer XOR operation (MDC link).

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
2

The ^ operator is bitwise XOR, you have more information in MDN: https://developer.mozilla.org/en/JavaScript/Reference/Operators/Bitwise_Operators

Yosi
  • 2,936
  • 7
  • 39
  • 64
2

That operator performs the logical XOR operation. (out bit is 1 when both input bits are different).

JTeagle
  • 2,196
  • 14
  • 15
1

This is the bitwise XOR operator, which returns a one for each position where one (not both) of the corresponding bits of its operands is a one. The next example returns 4 (0100):

Code: 
result = a ^ b; 
Kishore Kumar
  • 12,675
  • 27
  • 97
  • 154