Safely short circuiting a function
Often in code, you will see this pattern:
if (simpleTest) {
doSomething();
return;
}
However, often you would like your code concise. You could do this:
if (simpleTest) { doSomething(); return; }
But now you need to add curly braces (not that this generally matters). However you could also use this syntax:
if (simpleTest) return void doSomething();
This will execute doSomething() but will return undefined, so that you don't have to expose the internal state returned by doSomething() to your caller.
Written by Eric Anderson
Related protips
1 Response
This is a cool trick! Thanks
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Javascript
Authors
Related Tags
#javascript
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#