VAR  vs  LET  vs  CONST

VAR vs LET vs CONST

Why not VAR?

In general, the let keyword is used to declare variables. Although, there is another way to do it using the 'var' keyword. We find this in a lot of online JavaScript tutorials and blogs. We'll examine the var keyword in this article, and you'll learn about its drawbacks and reasons to stay away from it. So let's discuss Defining a function.

function numbers() {
    for (let i = 0 ; i < 6 ; i++ ) {
        console.log(i);
    }
}
numbers();

In the above code, i is only accessible inside { } the block. You will receive an error if you try to access i outside of the block. Saying i is not defined. See below code snippet for reference.

function numbers() {
    for (let i = 0 ; i < 6 ; i++ ) {
        console.log(i);
    }

    console.log(i);
}
numbers(); 


//Output//

ReferenceError: i is not defined

To be clear, even if I remove these curly braces, the error will remain the same.

function numbers() {
    for (let i = 0 ; i < 6 ; i++ ) 
        console.log(i);

    console.log(i);
}
numbers();

// output //

ReferenceError: i is not defined

Let's see what happens when we swap out "let" with "var." Apparently, 'I' is reachable outside of this block. The loop ends after the last iteration and displays the result. Hence, the problem with the var keyword is. A variable's scope is not constrained to the block in which it is defined when it is declared with the var keyword. It is restricted to the function level. It is one of those peculiar things that JavaScript has long possessed. From ES6, commonly known as ES 2015, two new keywords are introduced, let and const, to generate variables and constants. These two keywords produce block-scoped variables, whereas var creates function-scoped variables.

function numbers() {
    for (var i = 0 ; i < 6 ; i++ ) 
        console.log(i);

    console.log(i);
}
numbers();

// output //

0
1
2
3
4
5
6

There is another issue with Var concerning global variables. Avoid using global variables or use them sparingly in JavaScript. This is due to the ease with which other scripts can replace global variables. Although they are not inherently evil or particularly security-related, global variables shouldn't override the values of other variables. A maintenance problem might arise if we use more global variables in our code. Consider the case when the same-named variable was added. Prepare yourself for some significant bugs in that situation. Use local variables and enclose your code in closures to prevent using global variables. Var variables can be reassigned multiple times by re-declaring the variable.

In other words, One of the biggest problems with the var keyword for declaring variables is that it allows you to overwrite variables accidentally that you didn't mean to overwrite. Because usually, when you declare a variable with the keyword var or let, constant you're thinking this is the first time this variable is being declared, so if you were able to overwrite variables by doing this, you might accidentally overwrite variables that you don't intend to which is the biggest problem with var and let takes care of that problem because it won't let us do that ever, which will save us from making those simple mistakes.

Const and let are virtually identical, except that const prevents you from redeclaring the variable. Thus, as an example.

const name = 'praveen'
let name1 = 'alluri'

name = 'marthanda'
name1 = 'varma'

// output //

TypeError: Assignment to constant variable.

But const allows us to change the properties of an object. For an example

const person = {name :'praveen'}
let name1 = 'alluri'

person.name = 'marthanda'
name1 = 'varma'

console.log(person)

// Output//

{ name: 'marthanda' }

As you can see, we don't truly experience an error. Const thus only stops us from modifying the variable's actual value or assigning it a new value but not changing different properties in that value. Const and let are pretty comparable so you may use them both. However, unless you need to modify the variable's value, I would advise using const rather than let since you can be sure you won't mistakenly change the value if you choose not to. That's the essential difference between cost, var, and let.

Because of this and several other factors, I believe you should nearly and always use const and let when defining variables unless you have a specific situation for doing so. Even in that instance, I strongly advise you to consider why you want to use var. Because switching from using var to const or let in your design is always preferable.

That's all, folks

These are brief yet crucial lessons for our everyday work lives.

Learn more things, and share most stuff... Easy life with no worries.

Let's Rock!!!

Before you leave

If you like the content, please follow my social hangouts for exciting discussions.

References

links

Did you find this article valuable?

Support Yellow Multiverse by becoming a sponsor. Any amount is appreciated!