Yuma Yanagisawa
Yuma Yanagisawa

Reputation: 235

"|=" operator in JS (never seen before)

I just encountered the code below.

var loadComplete = 0;
window.onload = function(){
    loadComplete |= 1;
};

I've never seen "|=" like operator. Also I have googled about it, but there seems to be no example of it.

Upvotes: 2

Views: 84

Answers (1)

user1823
user1823

Reputation: 1111

Just as x += 1 is equivalent to x = x + 1, x |= 1 is the same as x = x | 1, where | is the bitwise OR operator.

Bitwise OR:

var a = 205;   // In binary: 11001101
var b = 45;    // In binary: 00101101
var c = a | b; // In binary: 11101101

Basically, if there are any ones in the column, it keeps it, and converts back to decimal notation, 237.

Upvotes: 9

Related Questions