25
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
  • 259,804
  • 351
  • 777
  • 1,080
  • 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

32

You have (at least) four/five options:

  1. In modern environments, you'd probably use the nullish coalescing operator (??):

    x = obj.key ?? "default";
    

    If obj.key results in undefined or null, "default" is used instead; if obj.key results in for any other value, that value is used (including various falsy values like "").

    In obsolete environments that don't have ??, you can use the curiously-powerful || operator:

    x = obj.key || "default";
    

    If obj.key results in any falsy value (including ""), that will use "default" instead; otherwise, it'll use the value. Where you can, you'll generally want to use ?? instead of || because it more accurately tests for a "missing" value.

  2. This is now obsolete, ?? handles this case. 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 (and obj.key) will find a key if it's in the object or the object's prototype chain. If you just want to check the object itself and not its prototype chain, in modern environments you can use Object.hasOwn:

    x = Object.hasOwn(obj, "key") ? obj.key : "default";
    

    In obsolete environments without hasOwn, you can use a polyfill, or use hasOwnProperty (but beware the object can have its own version of hasOwnProperty that may not tell the truth):

    x = obj.hasOwnProperty("key") ? obj.key : "default";
    
  4. If you don't want to exclude null, you can 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
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 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
  • 258,201
  • 41
  • 486
  • 479
2

Using destructuring assignment:

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

What about:

var d = {'hello':'abc'};
var helloVar = d.hello ? d.hello : 'default_val';
cutsoy
  • 10,127
  • 4
  • 40
  • 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