Loading
Lesson 10
Courses / JavaScript for Complete Beginners
JavaScript Variable Reassignment with let

Summary

NBK Tech World - Lesson Summary

In this lesson, we learn about assigning values to variables in JavaScript, focusing on the difference between const and let.

Key Points:

  1. Defining Variables:

    • Previously, we learned to define a variable using the const keyword. For example:
      const name = 'my name';
      
  2. Reassigning Values:

    • If you try to change the value of a variable defined with const, you will encounter an error:
      name = 'other name';  // Error: Assignment to constant variable
      
  3. Using let:

    • To allow reassignment of variable values, use the let keyword:
      let lastName = 'my last name';  // Initial assignment
      lastName = 'another name';  // Successful reassignment
      
  4. Differences between const and let:

    • const: The variable's value cannot be reassigned after its initial definition.
    • let: The variable can be reassigned multiple times.
  5. Best Practices:

    • Start with const for variables that will not change, and switch to let if reassignment is later needed.
  6. Naming Conventions:

    • Follow the camel case convention for variable names:
      • For example, anotherName where the first letter is lowercase, and the first letter of each subsequent word is uppercase.

Conclusion

Using const and let correctly allows for better management of variables in JavaScript, encouraging clear and maintainable code. Additionally, following naming conventions like camel case improves readability.

Thank you for watching, and see you in the next lesson!

Video Transcript

Welcome back to NBK Tech World. In this lesson we're gonna learn how to assign a value to a variable more than once. Previously we learned about the keyword cost to define a variable. We defined a variable name for example with your name as gave my name and we can use a variable later. I got this error because I had already defined name but it's here defined. So the problem now is what if I want to change the value of name. I want it to be something else somebody else's name how would I do it. What if I just try doing like so I say name again and whatever other name. You might notice because I already tried to do that before here and name has already been declared. Okay that's because I is cost let's see what we get if I do like so. So I get another error. I tried to assign the string other name to the variable name which was already previously defined with the value my name right using the cost keyword and we got an error saying assign it to constant variable was a type error whatever that means right. So actually the variable cost only allows you to assign a value to a variable once. If you try to reassign the value again later you will error out like so. So if you really want to be able to change the bad variables value later if you want to reassign it then you have to use the another keyword and that's called a let okay. So we're gonna let something be whatever. So let's try again let's make up a new variable. I'll call it say last name for example so we're gonna say let okay then the variable name I'm gonna say last name and give something my last name whatever okay. So this is gonna define a variable just like we learned before the variable is called last name and has the value my last name to confirm you can type it here. Just like before nothing changed but we did use the keyword let which will allow us as you're gonna see right now to reassign the value without any errors. So if I say last name is another name you say it worked no errors so I can say that's name and now the value is another name it totally changed okay. So that's the difference between lat and constant in JavaScript ES6. So using lat will allow you to reassign a value if you already assigned a value once you cannot do it again if you did use constant but if you use lat you can do it as many times as you want. So we reassigned last name to another name you can do it again. Last name is yet another name and it will work just fine and you can see the value of last name is now yet another name okay. So two ways to define a variable in JavaScript ES6 constant versus the lat constant. The rule like typically if I would do it I would always start with constant and define a variable of constant and then if I figure out a later I need a variable to change its value I would change that constant to a lat okay. So that's my own personal rule you can try that too. So I will start a constant if I find out a later on oh no I have to reassign the value to this variable second not use constant anymore I'm gonna switch and constant with lat. Alright and just to finish it off I would like to give you some convention let you know that notice I used the variable name last followed by uppercase name that's a camel case convention in the JavaScript that we use in JavaScript language so it's called a camel case. So for example if you have different words like I don't know another name like so you have two words right when I want to make a variable name my convention for naming the variable I will use another and then since I have another word name I capitalize the first letter and say like so. So this is called the camel case naming convention alright so another name and then becomes like that you have two words so for every new word that you add to the variable name capitalize first letter notice that the first letter very first letter of the variable name is lowercase not uppercase so it's a camel case okay so that's it for this lesson hope you all liked it and until the next time thank you so much for watching and the K-Tag world bye
No comments yet (loading...)
No comments yet (loading...)
Did you like the lesson? 😆👍
Consider a donation to support our work: