3

I usually use this code to see if the argument of a function is undefined or not

var = (typeof var != "undefined")? var : "othervalue"; 

but, other uses this boolean operator

var = var || "othervalue";

However, I have seen that if the value of checking and boolean false argument is not done correctly.

// assuming it is false
var = var || "othervalue"; // will be "OTHERVALUE"!!!

I need to know what exactly this operator in this context.

user1629569
  • 661
  • 1
  • 4
  • 17
  • 2
    I assume you meant to use one equal sign `=` as an assignment operator? – Brad Apr 02 '13 at 04:12
  • 1
    This is called [short circuit operators](http://en.wikipedia.org/wiki/Short-circuit_evaluation), which is different from strictly logical operators. – elclanrs Apr 02 '13 at 04:15
  • yep...6 a.m., corrected XD.... – user1629569 Apr 02 '13 at 04:15
  • This has got to be a dup, if only this was searchable... – Matt Ball Apr 02 '13 at 04:16
  • 3
    @MattBall: There is http://symbolhound.com/?q=%7C%7C+javascript. – elclanrs Apr 02 '13 at 04:18
  • 1
    possible duplicate of [What does the Javascript expression 'a = a || function() {...}' mean?](http://stackoverflow.com/questions/7069302/what-does-the-javascript-expression-a-a-function-mean) and http://stackoverflow.com/questions/1378619/javascript-operator and http://stackoverflow.com/questions/7118575/init-object-in-javascript-using-operator – Matt Ball Apr 02 '13 at 04:19
  • Brilliant, gentlemen and/or ladies. Never knew about symbolhound. – Matt Ball Apr 02 '13 at 04:20

3 Answers3

2

It returns the last expression that terminated the condition. It doesn't work the same as checking for typeof arg == "undefined", as any falsey value on the left will jump to the RHS.

alex
  • 479,566
  • 201
  • 878
  • 984
  • While this works, it seems like an abuse of the language. By evidence of the tone of the OP, it fails the Principle of Least Astonishment (http://en.wikipedia.org/wiki/Principle_of_least_astonishment) – PaulProgrammer Apr 02 '13 at 04:12
  • 2
    @PaulProgrammer it's fairly idiomatic JS. – Matt Ball Apr 02 '13 at 04:14
  • @PaulProgrammer JavaScript is missing a lot of things, such as default argument values. Using this pattern takes away *some* of the pain of not having this in the core (it's coming though). – alex Apr 02 '13 at 04:15
  • Alas, a decade and a half of dealing with languages like C and Java have totally ruined me for the coming wave of application development. – PaulProgrammer Apr 02 '13 at 04:17
1

|| operator will return the last expression, if first one is falsely:

var test  = first || "second";
// error will be raised, because first variable is not defined

var first;
var test  = first || "second";
// test   = "second", because first is falsely

var first = "first";
var test  = first || "second";
// test   = "first"

I always use ternary operator with typeof expression, because it's a really common thing to forget to define some variable:

var test  = 'undefined' != typeof(first) && first ? first : "second";
// test   = first if first is defined and true
IlyaDoroshin
  • 4,659
  • 4
  • 22
  • 26
0

I believe this is a perl style selection for first true (pseudocode below)

eat_breakfast = null
eat_dinner = null
eat_lunch = "eating lunch"

myVal = eat_breakfast || eat_dinner || eat_lunch
print myVal

would print "eating lunch"

it will set myVal to the first non-null/non-false entity.

Dmytro
  • 5,068
  • 4
  • 39
  • 50