Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save djD-REK/2c333deec31912e1971d4cffe2ce9071 to your computer and use it in GitHub Desktop.
Save djD-REK/2c333deec31912e1971d4cffe2ce9071 to your computer and use it in GitHub Desktop.
What Is The Short-Circuit Operator in JavaScript && (Logical AND) 1
// 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