3

Sorry if my question is silly, but i didn't find any sufficient answer on this.

I was creating a Facebook login just for training in my personal page. Studying on Internet i found that i need to provide some Facebook info like:

clientID: process.env.CLIENT_ID

So, i supposed that it would be (e.g)

clientID: process.env.8483843285375325blabla

But after struggling for a while, i figured out that the proper way was

    clientID: process.env.CLIENT_ID || '8483843285375325blabla'

So what the || stands for? When we use it? I have used it as an OR logical operator in other programming languages.

andyrandy
  • 72,880
  • 8
  • 113
  • 130
FotisK
  • 147
  • 1
  • 3
  • 10

3 Answers3

5

|| is the Javascript OR logical operator. This line:

clientID: process.env.CLIENT_ID || '8483843285375325blabla'

Can be read as: clientId attribute will be assigned the value of process.env.CLIENT_ID if logically true or '8483843285375325blabla'. So if process.env.CLIENT_ID is not set or set to false then '8483843285375325blabla' will be used.

yeiniel
  • 2,416
  • 15
  • 31
3

That is a normal logical or operator. But its usage is different at your context. It will start evaluate from left to right and whenever it faces a truthy value, it will break and return the detected truthy value.

var x = null || 0 || undefined || "" || "bringHimHome";
console.log(x) //bringHimHome

Consider the above code from left to right, all are falsy values, so it will evaluates one by one up to the last string and it will return that final string value as it is truthy.

Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130
1

|| is an logical or. Logical Operators

Returns expr1 if it can be converted to true; otherwise, returns expr2. Thus, when used with Boolean values, || returns true if either operand can be converted to true; if both can be converted to false, returns false.

Nina Scholz
  • 376,160
  • 25
  • 347
  • 392