Imagine, building an ecommerce website where you have to store product information. For each product, there must be a need to store the product name, manufacturer, date launched, quantities available and price. Now as a Developer, once you decide to store all this information, it becomes more and more difficult to remember where each piece of information is stored in memory because memory addresses on a computer are a long sequence of numbers and characters and it is impossible for any human to memorize them. Variables are used to name these memory locations so that when the information is needed again, one can just use the variable name and the information will be retrieved.
const. Initially, variables were created using only the
var keyword. But due to some historic drawbacks of using
const were implemented for creating variables. It is recommended that Developers don’t use the
var keyword anymore to create variables. Irrespective of which keyword you use to create a variable, the syntax still remains the same.
``` <keyword> <variable_name> = <value_to_be_stored> ```
As seen above, a variable can be created by specifying the keyword, followed by a variable name that will be used to store the value and as well retrieve it later, an equal sign for assigning values to the variable and the value to be stored itself.
Variable Naming Convention
- Variable name should only contain alphabets, numbers, $ and _
- Variable name should not start with a number
- Variable names are case-sensitive i.e.
Resultare two different variables
- Variables can’t be named as one of the reserved keywords like
- Variable should use
numOneis preferred over
- Variable can’t have hyphen
-in it’s name
- Use easy to understand names that symbolize the value stored in variables. For e.g. instead of calling a variable
phNum, a Developer can call it
- Don’t use single letter variable names like
Variables can be created using the
var keyword for variables is discouraged and should be replaced with
const keywords. Variables created using
var keyword are also function-scoped or global-scoped i.e. it is very hard to limit where in a large block of code the variable should be accessible. Hence, code written using the
var keyword is hard to maintain.
``` var numOne = 20; var numTwo = 30; var result = numOne + numTwo; console.log(‘Result is: ‘, result); ```
let keyword was introduced to solve hoisting issues that the
var keyword had.
let variables are block scoped and are only accessible to where they are declared. This limits the issues of variables being overwritten somewhere else in the code. Apart from this, the variables created using the
let keyword follow the same syntax as the ones created using the
var keyword. Variables created using
var keywords can be reassigned a value of different kind. Hence they are mutable.
``` let numOne = 20; let numTwo = 30; var result = numOne + numTwo; console.log(‘Result is: ‘, result); // should print 50 numThree = 60; result = numOne + numThree; // reassign result new value console.log(‘Result is: ‘, result); // should print 80 ```
Sometimes, variables created should not change the value assigned to it. This can’t be achieved if variables are created using
var keywords. In such cases, a variable should be created using the
const keyword. A variable created using
const can’t change the value assigned to it. It symbolizes constants.
``` let numOne = 20; let numTwo = 30; const result = numOne + numTwo; console.log(‘Result is: ‘, result); // should print 50 numThree = 60; result = numOne + numThree; // this is not allowed as result is a constant variable console.log(‘Result is: ‘, result); // this will not be executed as above line has error ```
const variables are also named differently sometimes when they store a value that would be otherwise hard to remember or store. Like private keys, colors, fonts, etc. usually have complex values and hence
const are appropriate for it.
``` const LIGHT_GRAY = ‘#ccc’; const DARK_GRAY = ‘#eee’; ```
Kick-Start Your Web Developer Career
We offer a wide variety of programs and courses built on adaptive curriculum and led by leading industry experts.
- Work on projects in a collaborative setting
- Take advantage of our flexible plans and scholarships
- Get access to VIP events and workshops
Recommended Courses for Web Developer
The Web Development bootcamp is an intensive, hands-on learning experience, designed to introduce the skills and concepts required to build modern web applications.
The part-time Web Development course is designed to provide a crash course in web development, with introductions to HTML, CSS, and the Bootstrap framework.