Javascript: Short-circuit evaluation

A strategy uses to avoid unnecessary work based on how some boolean operators are evaluated.

Image for post
Image for post
Photo by Linh Ha on Unsplash
if (!currency) {
currency = "$";
}
//short circuting with ||
currency = currency || "$";
if (emailId) {
login(emailId);
} else {
signUp();
}
//short circuting with && and ||
emailId && login(c) || signUp();
(function () {
'use strict';

function evaluateFirst(bool) {
console.log('first evaluation called -->', bool);
return bool;
}

function evaluateSecond(bool) {
console.log('second evaluation called -->', bool);
return bool;
}

var andWithFT = evaluateFirst(false) && evaluateSecond(true),
andWithTF = evaluateFirst(true) && evaluateSecond(false),
orWithTF = evaluateFirst(true) || evaluateSecond(false),
orWithFT = evaluateFirst(false) || evaluateSecond(true),
trinayWithTTF = true ? evaluateFirst(true) : evaluateSecond(false);

return [andWithFT, andWithTF, orWithTF, orWithFT, trinayWithTTF];
})();

If this post was helpful, please click the clap 👏 button below a few times to show your support! ⬇⬇

var everyDay = (Learning) => eval(new Me(Learning + Experience));

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store