Forked from djD-REK/What Is The Short-Circuit Operator in JavaScript && (Logical AND) 1.js
Created
December 17, 2020 23:17
-
-
Save DoctorDerek/00a8f9376c50f59825cb11ddf220f84b 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