What real life scenarios can this be used

https://www.codecademy.com/courses/introduction-to-javascript/lessons/control-flow/exercises/truthy-falsy-operators

Hello everyone,
In was real life scene can Short Circuit Evaluation which involves the logical OR to be used.
I see that a variable which has not been declared was called and it evaluate to a variable that was already declared.

If you’re on a path that includes using Express, then you’ll find a line like this in several projects:

const PORT = process.env.PORT || 4001;

This either assigns the port number set in the environment if it’s been configured or 4001. (The port is later used to tell Express what port to listen to requests on). Using Short Circuit Evaluation there saves us from having to do an if/else or using a ternary operator.

Without using that technique, it could be:

let PORT;
if (process.env.PORT) {
  PORT = process.env.PORT;
} else {
  PORT = 4001;
}

or

const PORT = process.env.PORT ? process.env.PORT : 4001;

There are other uses, of course. If the exercise included a function instead, it could look like this:

function mightier(writingUtensil) {
  writingUtensil = writingUtensil || 'pen';
  console.log(`The ${writingUtensil} is mightier than the sword.`);
}

mightier('marker');
// Outputs: The marker is mightier than the sword.

mightier();
// Outputs: The pen is mightier than the sword.

Notice that because I didn’t send an argument to the function, writingUtensil was assigned the value of pen.

Another way to accomplish that would have been to set a default value for the function parameter, which you’ll learn about later.

1 Like