Skip to content

Instantly share code, notes, and snippets.

@JH108
Created January 17, 2020 14:11
Show Gist options
  • Save JH108/5ded3ae902b9837e7a4949c6ba167c34 to your computer and use it in GitHub Desktop.
Save JH108/5ded3ae902b9837e7a4949c6ba167c34 to your computer and use it in GitHub Desktop.
Random stuff that is valid JS
var a = null;
function b(c) { var d = ['abc']; return d.find(e => e === c)}
if (Boolean(a)) console.log('True'); // True
if (!Boolean(a)) console.log('True'); // nothing
b('h') // undefined
b('abc') // abc
if (Boolean((a = b('abc')))) console.log('True', a); // True abc
if (Boolean((a = b('abcd')))) console.log('True', a); // nothing
@JH108
Copy link
Author

JH108 commented Jul 11, 2020

I think the goal here was to demonstrate variable reassignment within the evaluation of an if statement. The use case was that function b was an expensive function that I only wanted to run once but I needed to assign the result to variable a only if the function returned a valid result.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment