In JavaScript, var, let, and const are three different ways of declaring different types of variables. Previously there was only one way of declaring JavaScript variables using var keyword. In ES6, let and const keywords have been introduced that are used to declare block scoped variables. Currently most of the time we use let and const to declare variables.
Var - Declares current-scoped variables
Example:
try{
var no_of_months_in_year = 12;
(function () {
var no_of_days_in_week = 7;
console.log("===== Inside Function =====");
console.log("Number of days in a week is: " + no_of_days_in_week); //No Error
console.log("Number of months in a year is: " + no_of_months_in_year); //No Error
})();
console.log("===== Outside Function =====");
console.log("Number of months in a year is: "+no_of_months_in_year); //No Error
console.log("Number of days in a week is: "+no_of_days_in_week); //Error
}catch(e){
console.log("Error: " + e);
}
Output:
===== Inside Function =====
Number of days in a week is: 7
Number of months in a year is: 12
===== Outside Function =====
Number of months in a year is: 12
Error: ReferenceError: no_of_days_in_week is not defined
Let - Declares block-scoped variables
Example:
try{
let monthName = "January";
(function () {
let dayName = "Sunday";
console.log(monthName); //prints January
console.log(dayName); //prints Sunday
})();
monthName = "Fabruary";
console.log(monthName); //prints Fabruary
let monthName = "March"; // Error
}catch(e){
console.log("Error: " + e.message);
}
Const - Declares block-scoped constant variables
Example:
const number = 1234500;
console.log(number); //prints 1234500
try{
number = 5432100;
}catch(err){
console.log("Error: " + err.message);
//Error: Assignment to constant variable.
}
In this post you will be gaining valuable insights about the PHP sessions and it's working functionalities.
In this post you will be gaining valuable insights about the PHP Cookies and it's working functionalities.
This tutorial will explain you How to implement Singleton Design Pattern in PHP with the help of comprehensive examples.