Skip to content

Instantly share code, notes, and snippets.

@jonchurch
Last active February 28, 2023 03:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonchurch/ecf040b1543c1b2423ccb2f4913d076b to your computer and use it in GitHub Desktop.
Save jonchurch/ecf040b1543c1b2423ccb2f4913d076b to your computer and use it in GitHub Desktop.
Yoda style eslint rule example code
/*eslint yoda: "error"*/
const youveBecome = "powerful";
if ("powerful" === youveBecome) {
// yoda style
// value comes before variable
}
if (youveBecome === "powerful") {
// non-yoda
// variable comes before value
}
@jonchurch
Copy link
Author

This is an example of the eslint yoda rule.

"Yoda" style refers to putting the value before the variable you're comparing. It would read like "powerful equals what youveBecome", which sounds like how yoda speaks. Instead of "youveBecome equals powerful", which reads more naturally.

@maxqper
Copy link

maxqper commented Feb 28, 2023

Yoda speak is better. If there's a typo, compiler will catch it.

if ("powerful" = youvebcome) { // compiler error

if (youvebecome = "powerful") { // no error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment