21
d = {'hello':'abc'}
d.get('hello','default_val');

Above is python. How to do this in javascript? I want to be able to set a default value if no key found.

TIMEX
  • 238,746
  • 336
  • 750
  • 1,061
  • try with the answers given here: https://stackoverflow.com/questions/2631001/test-for-existence-of-nested-javascript-object-key – Martin Taleski Sep 04 '19 at 13:27

4 Answers4

28

You have (at least) four options:

  1. In many cases, you can use the curiously-powerful || operator:

    x = obj.key || "default";
    

    That means: Set x to obj.key unless obj.key is falsy, in which case use "default" instead. The falsy values are undefined, null, 0, NaN, "", and of course, false. So you wouldn't want to use it if obj.key may validly be 0 or any other of those values.

  2. For situations where || isn't applicable, there's the in operator:

    x = "key" in obj ? obj.key : "default";
    

    in tells us whether an object has a property with the given key. Note the key is a string (property names are strings or Symbols; if you were using a Symbol, you'd know). So if obj.key may be validly 0, you'd want to use this rather than #1 above.

  3. in will find a key if it's in the object or the object's prototype chain (e.g., all of the places you'd get it from if you retrieve the property). If you just want to check the object itself and not its prototype chain, you can use hasOwnProperty:

    x = obj.hasOwnProperty("key") ? obj.key : "default";
    
  4. Specifically check for undefined:

    x = typeof obj.key !== "undefined" ? obj.key : "default";
    

    That will use the default if obj doesn't have that property or if it has the property, but the property's value is undefined.

T.J. Crowder
  • 959,406
  • 173
  • 1,780
  • 1,769
  • Wow, never knew this. Does this work in other languages too (especially PHP)? It's really great. – cutsoy Jul 28 '11 at 07:50
  • 1
    @Tim: I don't know PHP well enough to answer that. In most languages (C, C++, C#, Java), `||` just returns `true` or `false`. JavaScript's version is more interesting and more in the "functional" style of programming. (BTW, `&&` does something similar: It gives you the thing on the right if the thing on the left is "truthy", or `false` if it isn't.) – T.J. Crowder Jul 28 '11 at 07:53
  • @TJCrowder, I just tested it and it doesn't work in PHP :( ... Thanks anyway =)! – cutsoy Jul 28 '11 at 07:57
  • 1
    @Tim: [apparently not in PHP](http://ideone.com/TNS1O) but it does in [Perl](http://ideone.com/HDywK) and [Ruby](http://ideone.com/ktbkW). – mu is too short Jul 28 '11 at 08:01
5

Javascript's logical OR operator is short-circuiting. You can do:

d["hello"] || "default_val";
Frédéric Hamidi
  • 249,845
  • 40
  • 466
  • 467
2

Using destructuring assignment:

const { hello = 'default_val' } = d
jsuth
  • 140
  • 1
  • 6
0

What about:

var d = {'hello':'abc'};
var helloVar = d.hello ? d.hello : 'default_val';
cutsoy
  • 9,979
  • 4
  • 38
  • 57
  • FWIW, that will work exactly like `helloVar = d.hello || 'default_val';` – T.J. Crowder Jul 28 '11 at 07:59
  • Yep, though `... || ...` is much shorter and more readable. Too bad it doesn't work in other languages, it would definitely made my day :p! – cutsoy Jul 28 '11 at 08:01