I'm fairly new to JavaScript. What does ||
do?
Asked
Active
Viewed 733 times
0

Ry-
- 218,210
- 55
- 464
- 476
-
mean or ........... – farmer1992 Oct 31 '13 at 03:52
-
2Search for ["javascript operators"](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators) (this works for most any language .. and Java != JavaScript). – user2864740 Oct 31 '13 at 03:53
-
http://stackoverflow.com/questions/18036171/proper-use-of – user2864740 Oct 31 '13 at 03:53
-
http://stackoverflow.com/questions/2100758/javascript-or-variable-assignment-explanation – pepsi Oct 31 '13 at 03:54
-
1Do you mean `Java || JavaScript`? (ps- it means "OR") – MadProgrammer Oct 31 '13 at 03:54
-
[Become a search ninja](http://stackoverflow.com/search?q=[javascript]+%22||%22) – Ry- Oct 31 '13 at 03:58
-
@minitech Cool, thanks for the heads-up about quotes (now?) working on symbols. – user2864740 Oct 31 '13 at 04:01
-
Become a search ninja... or maybe just read a JavaScript book. – Blue Skies Oct 31 '13 at 04:02
4 Answers
2
It is a condition operator, meaning “or”, typically used like this:
if (browserIsMSIE || browserIsFirefox) {
…
}

Roland Illig
- 40,703
- 10
- 88
- 121
2
MDN Expressions and Logical statements
(Logical OR) Returns expr1 if it can be converted to true; otherwise, returns expr2. Thus, when used with Boolean values, || returns true if either operand is true; if both are false, returns false.
var o1 = true || true; // t || t returns true
var o2 = false || true; // f || t returns true
var o3 = true || false; // t || f returns true
var o4 = false || (3 == 4); // f || f returns false
var o5 = "Cat" || "Dog"; // t || t returns Cat
var o6 = false || "Cat"; // f || t returns Cat
var o7 = "Cat" || false; // t || f returns Cat

Jay Harris
- 4,201
- 17
- 21
1
If something1 or something2, do something: equates to this
if (something1 || something2){
... do something
}
If something1 and something2, do something: equates to this
if (something1 && something2){
... do something
}

Paul Samsotha
- 205,037
- 37
- 486
- 720
1
It's same as in other C type languages. A Logical Operator, for the OR condition.
Here's the Docs on Mozilla Developer Network.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators

Calvin
- 423
- 3
- 12