cluster1
cluster1

Reputation: 5664

JavaScript - What does these value-assignment (using |= as an operator) mean?

Today I have seen these code-snippet:

  /**
     * @param src: any variable of any type
     * @param html: output format (true|false); default = false
     * @param level: (internal, don't use)
     *
     * @return string: formatted output
     */
    function showObj(src, html, level) {
      level |= 0;

Complete script: https://codereview.stackexchange.com/questions/123283/helper-function-to-format-output-any-type-of-variable

What is the value-assignment with |= ( in "level |= 0" ) ?

I haven't seen it anywhere before and can't find anything about it.

Upvotes: 1

Views: 49

Answers (2)

elreeda
elreeda

Reputation: 4597

The bitwise OR assignment operator uses the binary representation of both operands, does a bitwise OR operation on them and assigns the result to the variable.

Live Demo

var bar = 5;
bar |= 2; // 7

alert(bar)

Source

Upvotes: 2

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

Reputation: 67187

That(|) is a bit wise or operator, It normally used in situations where the decimal points of a number has to be truncated.

var level = 2.444434;
level |= 0; // level = level | 0;
console.log(level) // 2

Upvotes: 4

Related Questions