-4

How does work this operator is JavaScript. I often found this operator in two context:

//context 1
function(e){
e = e || window.event;

//context 2
if(a || b)

I know the type of return value of this operator in C or C++ is boolean. But I can't figure out what this operator does is JS.

Mat
  • 202,337
  • 40
  • 393
  • 406
frogatto
  • 28,539
  • 11
  • 83
  • 129
  • 2
    Maybe looking at the documentation would be helpful ? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators – Denys Séguret Aug 09 '13 at 09:52
  • The section in particular you want to read is `Short-Circuit Evaluation` –  Aug 09 '13 at 09:52
  • 2
    [**Exact duplicate of "Proper use of ||"**](http://stackoverflow.com/q/18036171/1348195) – Benjamin Gruenbaum Aug 09 '13 at 09:53
  • that documentation is useless in explaining how it works. It TRULY is useless. I've gone through basic Javascript training, which includes an explanation and examples of uses of the different operators. So I have a good foundation of javascript logic and how it's operators are used. I took a look at that reference because upon training myself on JQuery, the logical or operator was used in a way that made ABSOLUTELY no sense to me. So I checked that very reference that you linked...it did not help at all. It confused me a lot actually, as it didn't make sense with what I had learned. – Soundfx4 Apr 24 '15 at 16:23
  • 1
    @Soundfx4 I won't comment on the *"sacarstic asshats"* part of your comment. But if you read the link I gave you'll see it perfectly answers the question and is much more helpful than the answer here. You seem to be new here but please not that closing a question as duplicate is neither a punition nor *"picking on"* people. It's just normal handling of duplicate questions. – Denys Séguret Apr 24 '15 at 16:53

1 Answers1

3
e = e || window.event;

Means that if e doesn't coerce to true (typecast or loose comparison), it will instead try to set e to window.event

if(a || b)
{
    //code
}

Means, if a coerce to true (typecast or loose comparison), or b evaluates to true, then run the code

MDEV
  • 10,730
  • 2
  • 33
  • 49
  • True => truthy. For example `""` doesn't 'evaluate to true', however it does coerce to it. – Benjamin Gruenbaum Aug 09 '13 at 09:57
  • @BenjaminGruenbaum Yea, I know what I mean lol - I've reworded it to hopefully portray a clearer meaning. Not come across the term 'truthy' before, is that just representing a value that would be typecast to true? – MDEV Aug 09 '13 at 10:01
  • This is exactly what I was talking about in my comment above @dystroy. The documentation doesn't explain this particular use of the logical or operator at ALL as far as I could tell (and for that matter, confused me based on what I had already learned). And you, Smokey, explained it perfectly. I just recently saw the logical or operator being used to check a condition as per your first example, and that REALLY threw me off. I learned it as per your second example. Tyvm for explaining that. I couldn't find this answer on Google or the MDN documentation. – Soundfx4 Apr 24 '15 at 16:31