Variables are containers for storing data (storing data values).
In javascript, Before ES6 there were only one way of defining your variables: with the var keyword. If you did not define them, they would be assigned to the global object. Unless you were in strict mode, then you would get an error if your variables were undefined.
In JavaScript, There are two types of variables can be declared : local variable and global variable.
Variables declared out of any function are called global variables. They can be accessed anywhere in the JavaScript code, even inside any function.
Variables declared inside the function are called local variables to that function. They can only be accessed in the function where they are declared but not outside
<script>
var greet = "Hello " // global variable
function myfunction(){
var msg = "JavaScript!";
alert(greet + msg); //can access global and local variable
}
myfunction();
alert(greet); //can access global variable
alert(msg); //error: can't access local variable
</script>
<html>
<body>
<html>
<body>
<h1>Demo: JavaScript Global and Local Variables </h1>
In javascript ES6, there are three ways of defining your variables: var, let, and const. These keywords deliver different functionality from each other.
var x = 4.5;
var has a function scope, not a block scope.
In JavaScript, let is a keyword that is used to declare a block scoped variable. Other way, let is the block scoped version of var, and is limited to the block (or expression) where it is defined.
let x = 4.5;
var and let are both used for variable declaration in javascript but the difference between them is that var is function scoped and let is block scoped.
var variables can be re-declared and updated. This means that we can do this within the same scope and won't get an error.
let can be updated but not re-declared. This mean, a variable declared with let can be updated within its scope But a let variable cannot be re-declared within its scope.
ES6 introduced the const keyword, which is used to define a new variable in JavaScript. Generally, the var keyword is used to declare a JavaScript variable. Const is another keyword to declare a variable when you do not want to change the value of that variable for the whole program.
const is a variable that once it has been created, its value can never change.
const x = 16;
const has a block scope.
const does not define a constant value. const defines a constant reference to a value.
Do not use the var keyword inside a block (block scope). Always use let and const instead.
Once you've declared a variable with var or let, you can reassign a new value to the variable in your programming flow. It is possible if the variable is accessible to assign a value. But with const, you can't reassign a new value at all.
// Declare variables with initial values
let f_name = "Alex";
const ZIP = 560089;
var age = 25;
// Reassign values
f_name = "Bob"; // the f_name value is 'Bob"
ZIP = 65457; // Uncaught TypeError: Assignment to constant variable.
age = 78; // the age value is 78