What Does the "|" (Single Pipe) Do in JavaScript

What does the | (single pipe) do in JavaScript?

This is a bitwise or.

Since bitwise operations only make sense on integers, 0.5 is truncated.

x | 0 is x, if x is an integer.

Javascript conditional statement with a single pipe |

This is a bitwise OR operator. It will first convert it into a 32 bit integer, then apply the bitwise OR operation to the two numbers that result. In this instance, since Boolean(1) is true and Number(true) is 1, it will work fine without issue (the == operator will always return a boolean, and a if statement converts anything to a boolean). Here are a few examples of how it works:

1 | 0; // 1
0 | 0; // 0
0 | 1; // 1
1 | 1; // 1
true | false; // 1
false | false; // 0
2 | 1; // 3 (00000010, 00000001) -> (00000011)

As both sides have to be converted to a number (and therefore evaluated), this may cause unexpected results when using numbers when the logical OR statement (||) was meant to be used. For this, take these examples:

var a = 1;
a | (a = 0);
console.log(a); // 0

var b = 1;
b || (b = 0);
console.log(b); // 1

// I wanted the first one
var c = 3 | 4; // oops, 7!

Reference: http://www.ecma-international.org/ecma-262/5.1/#sec-11.10

What does the pipe(|) mean in typescript?

This is called union type in typescript.

A union type describes a value that can be one of several types.

Pipe (|) is used to separate each type, so for example number | string | boolean is the type of a value that can be a number, a string, or a boolean.

let something: number | string | boolean;

something = 1; // ok
something = '1'; // ok
something = true; // ok
something = {}; // Error: Type '{}' is not assignable to type 'string | number | boolean'

Playground


And here's an example similar to one in the question:

class Test1 {
public a: string
}

class Test2 {
public b: string
}

class Test3 {
}

let x: (typeof Test1 | typeof Test2)[];

x = [Test1]; //ok
x = [Test1, Test2]; //ok
x = [Test3]; //compilation error

x is an array containing constructors of Test1 or Test2.

What does the | operator do in JavaScript?

The pipeline operator (|>) calls its second operand (which should be a function) and passes its first operand as an argument to it.

That is,

arg |> func

is equivalent to

func(arg)

Its goal is to make function chaining more readable.


As it is now (in 2021), it's a non-standard and experimental thing created by Mozilla that works only in Firefox by enabling it explicitly.

However, because of the usefulness of this feature, there are two competing proposals in TC39, that would each add a different flavor of this operator to the language.

The exact syntax and semantics is to be determined, but in the simplest cases they will be similar to what's described here.


In Mozilla's variant (and the similar F#-style proposal) the code translated to this case will look like this:

const max = (_ => Math.max(..._))(
Object.values({a: 1, b: 2, c: 3})
)

console.log(max) //3

What is the use of | (pipe) symbol in a JS array

| is bitwise OR, which means, that all bits that are 1 in either of the arguments will be 1 in the result. A bitwise OR with 0 returns the given input interpreted as an integer.

In your code the its majorily used to convert the
Math.random()
number to integer. The bottom line is :

var a = 5.6 | 0 //a=5

What is the difference between the | and || or operators?

Just like the & and && operator, the double Operator is a "short-circuit" operator.

For example:

if(condition1 || condition2 || condition3)

If condition1 is true, condition 2 and 3 will NOT be checked.

if(condition1 | condition2 | condition3)

This will check conditions 2 and 3, even if 1 is already true. As your conditions can be quite expensive functions, you can get a good performance boost by using them.

There is one big caveat, NullReferences or similar problems. For example:

if(class != null && class.someVar < 20)

If class is null, the if-statement will stop after class != null is false. If you only use &, it will try to check class.someVar and you get a nice NullReferenceException. With the Or-Operator that may not be that much of a trap as it's unlikely that you trigger something bad, but it's something to keep in mind.

No one ever uses the single & or | operators though, unless you have a design where each condition is a function that HAS to be executed. Sounds like a design smell, but sometimes (rarely) it's a clean way to do stuff. The & operator does "run these 3 functions, and if one of them returns false, execute the else block", while the | does "only run the else block if none return false" - can be useful, but as said, often it's a design smell.

There is a Second use of the | and & operator though: Bitwise Operations.

What does |= (single pipe equal) and &=(single ampersand equal) mean

They're compound assignment operators, translating (very loosely)

x |= y;

into

x = x | y;

and the same for &. There's a bit more detail in a few cases regarding an implicit cast, and the target variable is only evaluated once, but that's basically the gist of it.

In terms of the non-compound operators, & is a bitwise "AND" and | is a bitwise "OR".

EDIT: In this case you want Folder.Attributes &= ~FileAttributes.System. To understand why:

  • ~FileAttributes.System means "all attributes except System" (~ is a bitwise-NOT)
  • & means "the result is all the attributes which occur on both sides of the operand"

So it's basically acting as a mask - only retain those attributes which appear in ("everything except System"). In general:

  • |= will only ever add bits to the target
  • &= will only ever remove bits from the target

Pipe (|) operator in Java

It's a bitwise OR operation. It's modifying things at a binary level.

             011                     3
in binary: | 100 in decimal: | 4
___ ___
111 7

Open Windows calc using scientific mode. You can flip between decimal and binary (and hex) and perform bitwise operations including or, and, xor, etc.

To do a bitwise or in your head or on paper, compare each digit of the same ordinal. If either number is a 1, the result at that ordinal will be 1.



Related Topics



Leave a reply



Submit