DEV Community

Asad Anik
Asad Anik

Posted on

What is JavaScript Short Circuit?

Let’s talk about JavaScript / React Falsely value ignorance. It’s call JavaScript Short Circuit Evaluation. In React we can learn and we also know about how to developing Applications. But We don’t know about some special things like JavaScript Short Circuit.

In JavaScript short-circuiting, an expression is evaluated from left to right until it is confirmed that the result of the remaining conditions is not going to affect the already evaluated result.

And there is 2 types of short circuits.

  1. And Short Circuit
  2. Or Short Circuit

And Short Circuit

When we are saying if something is there so do like this specific work or if you here then i will go. 🙄 I know that seems confusing here. So let’s see the real world example.

const iAmAStudent = true;
iAmAStudent && console.log('Yes you are a student!');
Enter fullscreen mode Exit fullscreen mode

So here we saw the and short circuit example, there is a log with iAmAStudent value when true. 😎

Or Short Circuit

When the decision is for just looking up choice who is 💝 present right now, then this will execute. This is actually the conditioning stages value.

const HOST = process.env.HOST || 'localhost';
console.log(HOST);
Enter fullscreen mode Exit fullscreen mode

Here we can see the useful example of or short circuit. If you have the value of process.env.HOST so okay it’s fine 🙃 but if you don’t has the .env value so it will set the ‘localhost’ for you.

Top comments (0)