Main point is that how to decide which one identifier should be used during development.
In java-script here are three identifiers.
1. var (Can re-declared & re-initialize)
2. const (Can't re-declared & re-initialize, can update array values by using push)
3. let (Can re-initialize but can't re-declare)
'var' : At the time of codding when we talk about code-standard then we usually use name of identifier which one easy to understandable by other user/developer. For example if we are working thought many functions where we use some input and process this and return some result, like:
**Example of variable use**
function firstFunction(input1,input2)
{
var process = input1 + 2;
var result = process - input2;
return result;
}
function otherFunction(input1,input2)
{
var process = input1 + 8;
var result = process * input2;
return result;
}
In above examples both functions producing different-2 results but using same name of variables. Here we can see 'process' & 'result' both are used as variables and they should be.
**Example of constant with variable**
const tax = 10;
const pi = 3.1415926535;
function firstFunction(input1,input2)
{
var process = input1 + 2;
var result = process - input2;
result = (result * tax)/100;
return result;
}
function otherFunction(input1,input2)
{
var process = input1 + 8;
var result = process * input2 * pi;
return result;
}
Before using 'let' in java-script we have to add ‘use strict’ on the top of js file
**Example of let with constant & variable**
const tax = 10;
const pi = 3.1415926535;
let trackExecution = '';
function firstFunction(input1,input2)
{
trackExecution += 'On firstFunction';
var process = input1 + 2;
var result = process - input2;
result = (result * tax)/100;
return result;
}
function otherFunction(input1,input2)
{
trackExecution += 'On otherFunction'; # can add current time
var process = input1 + 8;
var result = process * input2 * pi;
return result;
}
firstFunction();
otherFunction();
console.log(trackExecution);
In above example you can track which one function executed when & which one function not used during specific action.