DEV Community

Fabala Dibbasey
Fabala Dibbasey

Posted on • Updated 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! 🚀

Top comments (0)