Created
December 15, 2020 23:52
-
-
Save djD-REK/2c333deec31912e1971d4cffe2ce9071 to your computer and use it in GitHub Desktop.
What Is The Short-Circuit Operator in JavaScript && (Logical AND) 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Using && will prevent nonsense code from being executed. | |
false && console.log(NoNsEnSe_CoDE) // nothing happens | |
// You'll frequently see && used for type-checking. | |
const bananas = "🍌🍌🍌🍌🍌" | |
typeof bananas === "string" && console.log(bananas) // 🍌🍌🍌🍌🍌 | |
// For example, you might be expecting an array, not a string. | |
Array.isArray(bananas) && bananas.push("🍌") // nothing happens | |
// Using an in-line && operator is the same as an if statement. | |
if (bananas && bananas.length && bananas.length > 1) { | |
console.log(bananas) // 🍌🍌🍌🍌🍌 | |
} | |
bananas && bananas.length && bananas.length > 1 && console.log(bananas) | |
// Output: 🍌🍌🍌🍌🍌 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment