April 1, 2022 02:12 am GMT
Original Link: https://dev.to/moadhn/why-is-it-you-can-execute-a-function-on-the-right-side-of-an-and-operator-in-javascript-2633
Why is it you can execute a function on the right side of an AND && operator in JavaScript
I always assumed that operators like && and || are for comparing true and false statements on both sides.
But I'm seeing a pattern in React where, on the right side is a function and the left is a truthy statement that will then allow the right to be executed.
Example:
truthy statement && function
I understand that JavaScript will check this from left to right, so left being true will then allow it to check the right. But my assumption is the right should expect a true or false statement, but in this case it's just a function.
I'm so used to seeing the && operator in this context:
if (true && true) { // Execute this}
That this:
true && execute this
Works at all.
I guess my assumption is that JavaScript will just evaluate anything next to an operator whether it returns a true or false.
Original Link: https://dev.to/moadhn/why-is-it-you-can-execute-a-function-on-the-right-side-of-an-and-operator-in-javascript-2633
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To