DEV Community

Raj Kishor Shaw
Raj Kishor Shaw

Posted on

1

Var, Let, and Const

We all have used var, let, and const variables many times in JavaScript but sometimes we all get confused about which is the best variable to use for a particular program. I am writing this blog to understand let, var, and const variables for helping someone to clear their basics regarding the same.

Var

It is the oldest keyword to declare a variable in JavaScript. The scope of it is global or function scope. Variables defined outside the function can be accessed globally and variables defined inside a particular function can be accessed within the function.
It can be updated and re-declared into the scope.

Example -

    function f() {
        var a = 7;
        console.log(a)
    }
    f();
    console.log(a);
Enter fullscreen mode Exit fullscreen mode

Output-

7
ReferenceError: a is not defined
Enter fullscreen mode Exit fullscreen mode

Let

It is an improved version of the var keyword. The scope of a let keyword is only block scoped. It cannot be accessible outside the particular block.
It can be updated but cannot be re-declared into the scope.

Example 1-

    let a =7;
    function f() {
        if (true) {
            let b = 9

            // It prints 9
            console.log(b);
        }

        // It gives error as it
        // defined in if block
        console.log(b);
    }
    f()

    // It prints 7
    console.log(a)
Enter fullscreen mode Exit fullscreen mode

Output-

9
ReferenceError: b is not defined
7
Enter fullscreen mode Exit fullscreen mode

Example 2-

let a = 7
if (true) {
    let a=9
    console.log(a) // It prints 9
}
console.log(a) // It prints 7
Enter fullscreen mode Exit fullscreen mode

Output-

9
7
Enter fullscreen mode Exit fullscreen mode

Const

It has all the properties same as the let keyword, except the user cannot update it. It cannot be updated or re-declared into the scope.

Example-

    const a = {
        prop1: 7,
        prop2: 9
    }

    // It is allowed
    a.prop1 = 3

    // It is not allowed
    a = {
        b: 7,
        prop2: 9
    }

Enter fullscreen mode Exit fullscreen mode

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (1)

Collapse
 
decker67 profile image
decker

When to use what would be interesting: const -> let, never var.

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

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

Okay