Why use let if you can and var

Tell us what’s happening:
Describe your issue in detail here.

Why use let when var code works [for (var i = 0; i < contacts.length; i++)]

   **Your code so far**

// Setup
var contacts = [
   {
       "firstName": "Akira",
       "lastName": "Laine",
       "number": "0543236543",
       "likes": ["Pizza", "Coding", "Brownie Points"]
   },
   {
       "firstName": "Harry",
       "lastName": "Potter",
       "number": "0994372684",
       "likes": ["Hogwarts", "Magic", "Hagrid"]
   },
   {
       "firstName": "Sherlock",
       "lastName": "Holmes",
       "number": "0487345643",
       "likes": ["Intriguing Cases", "Violin"]
   },
   {
       "firstName": "Kristian",
       "lastName": "Vos",
       "number": "unknown",
       "likes": ["JavaScript", "Gaming", "Foxes"]
   }
];


function lookUpProfile(name, prop) {
 // Only change code below this line
for (let i = 0; i < contacts.length; i++) {
   if (contacts[i].firstName === name) {
     if (prop in contacts[i]) {
       return contacts[i][prop];
     } else {
       return "No such property";
     }
   }
 }
 return "No such contact";
 // Only change code above this line
}

lookUpProfile("Akira", "likes");
   **Your browser information:**

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36

Challenge: Profile Lookup

Link to the challenge:

Because “let” keeps the variable to a small scope.
If you use “var”, you can get some unexpected behavior.

Using “var” in a for-loop actually creates the variable for a bigger scope - meaning you can still access it once the loop is concluded. “let” on the other hand will limit it just to the loop.

 for (var i = 1; i<=3; i++){}
 var i
 console.log(i)

This code will say “i” would have the value of 4. Which can mean trouble if I the loop is like hundreds of lines away and I assume I just declared it without assigning a value, so it must be “undefined”.

1 Like

@mhtiragla ,
variable defined with var can be redefined also value of the variable can be changed.
variable defined with let can not be redefined but value of the variable can be changed.
variable defined with const can not be redefined also value of the variable can not be changed.

it is a good practice to use let and const while writing the code, in larger projects where there will be thousands of lines of code and multiple working on same code base, the variables are strictly defined by const to avoid troubles of changing and redefining the variables written by others.
E.g.: Suppose you joined a new project, there will be existing code base contains thousands of lines of code which you didn’t write and so you don’t have idea what code is written. So when you get a task, you might end up redefining the variable same as that of existing name which will cause problems.

1 Like

TLDR: Let is the most modern and safer way to initialize the variables instead var.

“var” was the first way to initialize variables on Javascript. Then “let” arrived on ES6.
Take var as initializing a global variable (every function can use it) and let as a local one (it’s more strict and local scoped).

What @Jagaya says. There are differences, but the main thing to understand IMO is:

  • there are a some problems with how var works in JavaScript. These have always been there from when the language was created.
  • those problems cannot be fixed. The way JavaScript currently works cannot be changed. Doing that would break existing code on the internet.
  • JavaScript doesn’t have versions: there isn’t a way to say “this code uses JS version 1, so to run this website you need that installed rather than JS version 2, because var works differently in version 2”. There is just JavaScript.
  • the only way to fix the problems with var is to add a new keyword to the language with the new behaviour. So let and const now exist. let does basically the same thing as var but fixes a few of the problems.
  • so where you would use var before the new keyword existed, use let (or const, but in this case has to be let)
1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.