In any product, we have to store the information that belongs to the product.
we have Rs.100/-, we need a locker to store that Rs.100/- and a bank should make that locker for us.
In Javascript, it works in the same way:
To store Rs.100/- we need a variable and we can make variable by using var, let and const.
we can store any type of information in a variable. Think of a variable as a box which stores any type of information.
1var locker; // this statement creates a variable and named it as a locker.
In above statement var is a bank which maker locker for us.
1var locker = 100; // here we declared a variable named locker and assigned 100 to it.
The other way of doing it.
1let locker = 100;2const locker = 100;
we can type variable name to access the value stored in it. if we type locker it gives us value 100
To change the value 100 to 200 we can simply write
1locker = 200; // here we reassigned value 200 to variable locker
we can declare a new variable and assign another variable value to it
1var another = locker; // here we declared a variable named other and assigned value of locker
To check enter
1another; // which shows us 200 as value
About var, let and const
var:
- We can declare a variable & assign a value
- We can redeclare a variable & reassign a value
- With var, it initializes the value of undefined in declaration mode & it assigns value in execution mode
let:
- We can declare a variable & assign a value
- We can reassign a value
- With let, it won’t initialize any value in declaration mode and It assigns value in execution mode
const:
- We can declare a variable
- We can assign a value
- With const, it won’t initialize any value in declaration mode and It assigns value in execution mode
We can also declare multiple variables in one line:
1var name = "stark",2 age = 22,3 gender = "male";
The above statement normally we can write like
1var name = "stark";2var age = 22;3var gender = "male";
Variable naming: There are two limitations on variable names in JavaScript: The name must contain only letters, digits, or the symbols \$ and _. The first character must not be a digit. eg:
1var firstName = "bran stark";2var you123 = 45;34var 123test = "bran stark" // invalid because we can’t use number as first charcter;5var first-name = "bran stark" // invalid because we can’t use hyphen in variable names
If we want to declare names with more words normally we follow camelCase. eg:
1var yourFirstName;
In naming variable case matters. if two variables with the same name but different case are different variables eg:
1var name;2var Name; // it is different variable from above
Reserved names: We can’t use some names like function, new, let, class … and there so many, we will find in javascript laterin our learning phase
Naming things in the right way: Always a variable name should be clean, readable and describing the data it stores. for example, we want to store a name in a variable.
1var a = "bran stark"; // wrong convention2var userName = "bran stark"; //meaningful
Variable naming is one of the most important and complex skills in programming. A glance at variable names can reveal which code was written by a beginner versus an experienced developer.