DEV Community

Fabala Dibbasey
Fabala Dibbasey

Posted on • Edited on

Variables, var, const, and let in conversation

Unveiling the Drama: Variables, const, let, and var in JavaScript—A Riveting Conversation!

Variables:

Hey there! If you think I'm just a container, you're missing the big picture. Greetings, my components! Without you, my declaration is incomplete.

var:

Hello 'const' and 'let'! Hey 'let,' you've been getting all the attention lately, but what makes you so special?

let:

Umm, well...

const:

Hold on, 'let'! Let me introduce myself first. I am the 'const' keyword, the guardian of constant variables. Once declared, there's no turning back.

let:

I declare variables, and yeah, they can be reassigned everywhere in your program. I see all scopes, by the way.

var:

Oh, 'let', I only see function scope. How can we simplify things for developers?

let:

Take a look at this demo:

function diffVarAndLet() {
    // Extra scope
    if (true) {
        let x = "Look, I'm in a scope";
        // 'let' -> My life starts and ends only in this scope
        var y = "See, I don't care about scope!";
    }

    console.log(y); // Output: See, I don't care about scope!
    console.log(x); // Remember, I warned you about my life! Output: ReferenceError: x is not defined
}
Enter fullscreen mode Exit fullscreen mode

In short, I'm here to solve a problem you didn't even know existed—scopes!

var:

Excellent! Thank you. Together, we can elevate JavaScript. If you're new to JavaScript, go with 'let'—it rarely disappoints. Only the intermediates and experts truly understand when I'm needed.

const:

Variable, you claim not to be a container—so what are you?

Variable:

I'm used to mapping to a value in memory for reference purposes."

Get ready for an exhilarating journey through the world of JavaScript variables! 🚀

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay