Skip to content
DevMeme
3928 of 7435
Yoda on Modern JavaScript: The Var Awakens No More
Languages Post #4275, on Mar 16, 2022 in TG

Yoda on Modern JavaScript: The Var Awakens No More

Why is this Languages meme funny?

Level 1: Use let, you must

Imagine you have two ways to do something, one new and one old. The new ways are safe and tidy, and the old way is a bit messy and can cause problems. This meme is like a wise teacher (Yoda, the green Jedi Master) telling you: “Use the good ways, or don’t use the bad way at all.” It’s similar to a parent saying, “Wear your helmet or you’re not riding your bike – there’s no riding without a helmet.” In simple terms, Yoda is advising developers to use the safer, better tools (let or const) and completely avoid the old risky one (var). It’s funny because Yoda’s treating a coding tip as an ultimate rule of the universe. Basically, even a Jedi Master thinks the newer method is the only way to go!

Level 2: A New Scope

This meme is basically a clever way of saying: In JavaScript, use let or const when you make a variable, and don’t use var at all. It’s referencing a famous line by Yoda from Star Wars. In the movie, Yoda advises Luke, “Do or do not. There is no try.” That was Yoda telling someone to commit fully to an action rather than doing it half-heartedly. The meme swaps in JavaScript terms: “let or const. There is no var.” It means when you’re coding, choose either let or const for your variables – don’t even consider using var. It’s a fun way to frame a language best practice (a recommended way of writing code) as if it were age-old Jedi wisdom.

To understand the joke, you need to know what var, let, and const are:

  • JavaScript: a very popular programming language, especially for making web pages interactive. It has several ways to create a variable (a named container for a value in your code).
  • var: The old way to declare a variable (from the earliest versions of JavaScript). If you use var, that variable is either global or tied to the entire function it’s in, not just a small block. This can make things confusing. For example, if you do var weather = "rain" inside an if statement, you can still use weather outside that if later on. It doesn’t stay limited to the if block.
  • let: A newer way (introduced in 2015 with ES6, a major JavaScript update) to declare a variable. Variables declared with let are block-scoped, meaning they exist only inside the block { } where you define them. If you declare let mood = "happy" inside an if block, it won’t exist outside that block. This behavior is usually what you expect and helps prevent mistakes.
  • const: Another new way introduced alongside let. A const is also block-scoped but additionally is constant – once you assign a value to a const variable, you can’t reassign it later. For example, const pi = 3.14; keeps the value 3.14 forever (you aren’t allowed to do pi = 4 later; the code would throw an error). You use const for variables that shouldn’t change, like configuration values or formulas. If you try to change a const, JavaScript will stop you, which is actually helpful for catching errors.

So why does Yoda (and basically every modern developer) say forget var? Because var’s old behavior can easily cause bugs or confusing code. Beginners might not realize, but var doesn’t play by the same rules as let/const. Here’s a common scenario:

if (true) {
  var snack = "apple";
}
console.log(snack); // Prints "apple" – var escapes the if block!

Even though snack was declared inside the if block, using var made it as if it was declared at the top of the function (or global scope). That’s why console.log could still see it. This might surprise someone who assumed snack would go away after the if. If we use let instead:

if (true) {
  let drink = "water";
}
console.log(drink); // Error! drink is not defined outside the block

Using let, the variable drink exists only inside the if braces. Outside the block, it’s as if drink never existed – which is usually what we want for keeping our code tidy and predictable. With var, having variables sneak out like that can lead to unintended side effects (maybe you reuse the name snack elsewhere not realizing it’s still around).

Another issue is that var can be redeclared. If you write var count = 1; and later in the same function do var count = 2;, JavaScript won’t complain – it’ll just override the variable. This can make it hard to track what the current value of count is or if you meant to change it. In contrast, doing let count = 1; and then let count = 2; in the same scope would produce an error – a good thing, because it catches the mistake of using the same name twice. You’d be forced to either reuse the existing count or choose a new name, making your intentions clearer.

From a CodeQuality perspective, let and const help you write cleaner, less error-prone code. Many coding tutors, courses, and style guides will tell newbies: “Always use let or const, never var.” In fact, modern code editors and tools will warn you if you type var. It’s like a spellcheck flagging a word – the tool is saying “Are you sure? var is kind of outdated.” This is why the meme strikes a chord: it’s exaggerating that advice to “never use var” by putting it in Yoda’s strict, knowing voice.

The Star Wars reference (Master Yoda giving advice) makes it fun, but the takeaway for a junior developer is real: use the newer variable declarations (let and const) for virtually everything, and you can safely ignore var in modern JavaScript programming. It will make your life easier down the road. As Yoda might rephrase for coders: “Train yourself to let go of everything you fear to lose – especially var.” 😉

Level 3: The Phantom Hoisting

Even the wisest Jedi Master Yoda seems to have opinions on modern JavaScript best practices. The meme shows Yoda proclaiming, “let or const. There is no var.”, remixing his famous Star Wars mantra into a coder’s advice. This tongue-in-cheek declaration reflects a widely accepted language best practice in the JavaScript community: since ES6 (ECMAScript 2015), developers are urged to use the newer let or const for variable declarations and abandon the old var entirely. It’s as if Yoda is laying down an absolute code law in the same way he’d teach about the Force.

Var leads to confusion, confusion leads to bugs, bugs lead to suffering.” – (not) Yoda

This parody quote captures why experienced devs caution against var. In the past, many of us were burned by var’s odd scoping behavior and hoisting (the way var declarations float to the top of their scope). Over years of debugging and a few late-night curses, best practice evolved to favor let and const. The meme gets a laugh from seasoned programmers because it dramatizes this hard-earned wisdom as Jedi-level doctrine. Yoda’s stern advice is funny and true: in modern code, there really is no good reason to reach for var anymore.

Let’s break down why this is such a big deal in the JavaScript ecosystem:

  • Function Scope vs Block Scope: A variable declared with var is scoped to the function (or globally, if outside any function), ignoring block boundaries like if or for. This means a var inside an if{...} still exists outside of it, which can be unintuitive. In contrast, ES6 introduced let and const which are block-scoped – they only exist within the nearest set of braces { }. This prevents variables from leaking out and causing accidental interactions in other parts of the code.
  • Hoisting and the Temporal Dead Zone: JavaScript “hoists” var declarations: the interpreter moves them to the top of their scope and initializes them with undefined. This can lead to weird scenarios where you use a var variable before it’s written, yet no error occurs (it just reads as undefined). let and const are hoisted too in theory, but not initialized immediately; accessing them before their line of declaration throws an error. This period between the top of scope and the actual declaration line is nicknamed the Temporal Dead Zone (TDZ) – a fancy way of saying “you can’t use it until it’s been declared.” The TDZ helps catch mistakes: if you accidentally use a let variable too early, you’ll know immediately. With var, you might silently get undefined and then scratch your head later.
  • Re-declaration and Safety: With var, you can declare the same variable name more than once in the same scope without error. The last declaration just overwrites the previous value, which can be a source of bugs (did I mean to re-use that name?). let and const throw an error if you try to declare a variable with an existing name in the same scope. This strictness is good – it forces you to pick unique names or use a single declaration, making the code clearer. It’s like the language yelling “Hey, you already have a variable for that!” instead of quietly doing something surprising.
  • const for Constants: On top of block scope, const introduces the idea of an immutable reference. Variables declared with const must be assigned once and then never reassigned. This is great for values that shouldn’t change – it makes your intent clear and prevents accidental changes. With var, you had no such guardrail; any var could be reassigned or even redeclared at will. Using const heavily (and let for the rest) makes your code more self-documenting and robust.

All these improvements mean code with let/const is usually less error-prone and easier to understand than code with var. No more worrying that a loop variable declared with var inside one block might mysteriously still exist elsewhere, or that using a name twice might silently override something important. It’s a quality-of-life upgrade for developers. In fact, many codebases and linting tools (like ESLint) enforce a “no-var” rule – if you attempt to check in var somewhere, you’ll get flagged faster than the Millennium Falcon doing the Kessel Run. It’s almost a shibboleth: using var in new code is a hint you learned JavaScript in the distant past (the “Old Republic” era of JS).

To illustrate one notorious problem var can cause, consider the behavior of a loop with asynchronous callbacks:

for (var i = 1; i <= 3; i++) {
  setTimeout(() => console.log('var loop:', i), 0);
}
for (let j = 1; j <= 3; j++) {
  setTimeout(() => console.log('let loop:', j), 0);
}

// Sample output:
// var loop: 4  
// var loop: 4  
// var loop: 4        (all three logs show 4 – var 'i' is hoisted & shared, ends as 4 after loop)
// let loop: 1  
// let loop: 2  
// let loop: 3        (logs 1, 2, 3 – each loop iteration had its own 'j' because of block scope)

In the var loop above, the intention was to print “1, 2, 3”, but because var isn’t block-scoped, the variable i inside the loop is the same variable throughout. By the time the setTimeout callbacks run, the loop has finished and i is left at 4 (the loop ends when i increments past 3). Thus it prints “4” three times – likely not what you wanted. This kind of bug has tripped up many developers and feels as frustrating as a yo-yo that mysteriously unrolls itself. By using let for the loop variable j, each iteration gets a fresh j that exists only for that loop cycle, so the callbacks correctly print “1, 2, 3”. The difference is literally just the keyword, but it alters the behavior in a fundamental way. It’s a powerful demonstration of why “there is no var” in modern JS code – using let (or const when values shouldn’t change) prevents this class of bug entirely.

In short: Yoda’s humorously strict guidance aligns with what every senior JavaScript developer eventually learns. var is an old construct from JavaScript’s earlier days (when the language was young and maybe a tad clumsy with scoping). As the language matured (and developers suffered enough CodeQuality issues), let and const emerged as the new standard for variable declaration – bringing order to the Force, so to speak. The meme’s comedy comes from how absolute the advice is (“There is no var”) and the choice of spokesperson: a legendary wise character who always speaks in absolutes like a teacher laying down universal truth. It exaggerates a real sentiment in the dev community to the level of almost spiritual dogma, which is funny and relatable. After all, if even Master Yoda says it, you know it’s a rule to follow!

Description

This meme features a still image of the wise and pensive character Yoda from Star Wars. Above his head is a text overlay presented as a quote: '"let or const. There is no var." -Yoda'. This is a clever parody of Yoda's famous line, 'Do or do not. There is no try.' The joke directly maps this iconic philosophical advice to a modern JavaScript best practice. In contemporary JS (ES6 and beyond), 'let' and 'const' are the preferred keywords for variable declaration due to their block-scoping and immutability features, respectively, which prevent common bugs associated with the older, function-scoped 'var'. For experienced developers, this meme is a humorous nod to the evolution of the language and the dogmatic adherence to new standards, framing the abandonment of 'var' as a matter of Jedi-like discipline and wisdom

Comments

26
Anonymous ★ Top Pick Using 'var' in a modern codebase is a pathway to many abilities some consider to be unnatural, like hoisting bugs and scope pollution
  1. Anonymous ★ Top Pick

    Using 'var' in a modern codebase is a pathway to many abilities some consider to be unnatural, like hoisting bugs and scope pollution

  2. Anonymous

    Yoda’s right: choose let or const; var is the Phantom Menace of JavaScript - hoisted, over-scoped, and guaranteed to make your future self stare at the diff like Obi-Wan watching Anakin become technical debt

  3. Anonymous

    After 15 years of debugging production issues caused by var's function-scoping and hoisting quirks, senior engineers have achieved Jedi-level enlightenment: the real dark side isn't the Sith, it's explaining to a junior why their for-loop iterator just leaked into global scope and overwrote the CEO's dashboard metrics

  4. Anonymous

    Nine hundred years of hoisting bugs has Yoda seen; function-scoped, his patience is not

  5. Anonymous

    Yoda's wisdom transcends galaxies: in modern JavaScript, 'var' is as outdated as using the Force without proper error handling. Experienced engineers know that 'let' and 'const' aren't just syntactic sugar - they're essential for avoiding the hoisting nightmares and function-scoped chaos that haunted pre-ES6 codebases. Block scoping and immutability by default? That's the path to maintainable code, young Padawan. The real Jedi move is defaulting to 'const' unless reassignment is truly necessary, because mutation leads to bugs, bugs lead to debugging sessions, and debugging sessions lead to suffering

  6. Anonymous

    Write var and you summon the dark side of hoisting; let/const and the temporal dead zone keep your for-loop callbacks from all believing i = 10

  7. Anonymous

    Yoda gets it: 'var' hoisting is the dark side that turns local vars into global nightmares

  8. Anonymous

    ‘var’ is basically a production incident dressed as a keyword - function‑scoped, hoisted, and guaranteed to violate your SLOs; use let/const you must

  9. @Agent1378 4y

    Is this some JS meme?

  10. @mr_oz 4y

    It’s russia meme, because if you say war you go to jail for 15years

    1. @lovetraindriver 4y

      Heh.

    2. @zexUlt 4y

      You should now say "vvar"

      1. @mr_oz 4y

        I am can say all I want because I leave in democratic country not in russland

        1. @RiedleroD 4y

          *russia

          1. @mr_oz 4y

            Fckrussia

            1. @sylfn 4y

              Fuck-ussia* [Please use English here]

              1. @mr_oz 4y

                Updated)

    3. @thematdev 4y

      it is not true

  11. @asm3r 4y

    Function- var. Class - let. You should know how var works even in 2022

    1. @RiedleroD 4y

      function a(){ let x = 2; return x; }

      1. @asm3r 4y

        Where a visible?

        1. @RiedleroD 4y

          idk, globally?

          1. @asm3r 4y

            Yep, you don't know var scoping

            1. @RiedleroD 4y

              never said I did lol I hate js with a passion

    2. @azizhakberdiev 4y

      Xcuse me, what the fuck?

      1. @asm3r 4y

        Programming.

Use J and K for navigation