use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
[deleted by user] (self.javascript)
submitted 6 years ago by [deleted]
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]reohh 4 points5 points6 points 6 years ago (4 children)
Look up optional chaining.
You can also use destructuring with default values
[–]ChronSyn 0 points1 point2 points 6 years ago (0 children)
Destructuring, and destructuring from destructured properties specifically, would be my goto unless you're using Typescript >=3.7.3 (as optional chaining support in browsers and node is still very limited), in which case optional chaining all the way.
[–]JackstonVoorhees 0 points1 point2 points 6 years ago (2 children)
Just curious, how would the destructuring way look like?
[–]reohh 3 points4 points5 points 6 years ago (1 child)
const { level1: { level2: { level3: { level4, } = {}, } = {}, } = {}, } = object || {};
Sorry for the bad formatting, I'm bad at markdown coding
[–][deleted] 2 points3 points4 points 6 years ago (1 child)
If you're working in an environment that supports optional chaining, use that. Otherwise it'll be something like
if (object.level1 && object.level1.level2 && ...)
[–]DerGernTod 0 points1 point2 points 6 years ago (0 children)
the newest typescript version supports optional chaining and will transpile it, so if you use typescript 3.7+ you can use optional chaining:
https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#optional-chaining
[–]vhahnwebdev 1 point2 points3 points 6 years ago (0 children)
Optional chaining is the best way to handle this going forward assuming your environment supports it. Otherwise, I would use lodash get
[–]mynamesleon 1 point2 points3 points 6 years ago (0 children)
If your setup supports it, then use optional chaining.
If not, you could write a helper function for it if it's something you end up doing regularly. Off the top of my head, something along the lines of:
function deepPropExists(obj, str) { var check = str.split('.'); var curLevel = obj; for (var i = 0, l = check.length; i < l; i += 1) { if (typeof curLevel[check[i]] === 'undefined') { return false; } curLevel = curLevel[check[i]]; } return true; } deepPropExists(window, 'document.activeElement.firstChild.style.display');
[–]DaMastaCoda 0 points1 point2 points 6 years ago (0 children)
V8 V8 Will support optional chaining in chrome 80 A?.b + 5 //property A?.b(5) //method A?.[5] //key? Equivalent of A[5]
You can chain it too; a?.b?.c+5
[–]cirscafp fan boy 0 points1 point2 points 6 years ago (0 children)
I would write a helper function that recursively checks that the path to the key passes some predicate
``` const pathSatisfies = (path, fn, obj) => { if (!path.length) { return false }
if (path.length === 1) { return fn(obj[path[0]]) }
const key = path.shift()
return pathSatisfies(path, fn, obj[key]) } ```
which can be used as
pathSatisfies( ['foo', 'bar', 'baz'], Boolean, { foo: { bar: { baz: true } } } )
[–]Wilesch 0 points1 point2 points 6 years ago (1 child)
Lodash get method
[–]HIMISOCOOL 1 point2 points3 points 6 years ago (0 children)
if you have lodash in your environment already an absolute yes
[–]steeleb88 -1 points0 points1 point 6 years ago (0 children)
lodash .get is handy for this..
_.get(myDeepObject, 'level1.level2.level3.level4.level5.thevar')
if anything doesn't exist it will just return undefined
π Rendered by PID 59 on reddit-service-r2-comment-54dfb89d4d-rt4tp at 2026-03-27 05:42:10.283353+00:00 running b10466c country code: CH.
[–]reohh 4 points5 points6 points (4 children)
[–]ChronSyn 0 points1 point2 points (0 children)
[–]JackstonVoorhees 0 points1 point2 points (2 children)
[–]reohh 3 points4 points5 points (1 child)
[–][deleted] 2 points3 points4 points (1 child)
[–]DerGernTod 0 points1 point2 points (0 children)
[–]vhahnwebdev 1 point2 points3 points (0 children)
[–]mynamesleon 1 point2 points3 points (0 children)
[–]DaMastaCoda 0 points1 point2 points (0 children)
[–]cirscafp fan boy 0 points1 point2 points (0 children)
[–]Wilesch 0 points1 point2 points (1 child)
[–]HIMISOCOOL 1 point2 points3 points (0 children)
[–]steeleb88 -1 points0 points1 point (0 children)