What is variables (var, let, const) in JavaScript?

In JavaScript, variables are containers that store data values. They can be declared using three different keywords: var, let, and const.

var: This keyword is used to declare a variable that is function-scoped or globally scoped. It allows for variable redeclaration and hoisting.

let: This keyword declares a block-scoped variable, meaning it is limited to the block in which it is defined. It does not allow redeclaration within the same scope.

const: This keyword is used to declare a block-scoped variable that cannot be reassigned. A constant's value must be assigned at the time of declaration and cannot be changed.

Here is an example that demonstrates the use of these variable declarations:

// Using var var name = 'John'; console.log(name); // Output: John // Using let let age = 30; age = 31; // Allowed console.log(age); // Output: 31 // Using const const birthYear = 1993; console.log(birthYear); // Output: 1993 // birthYear = 1994; // This will throw an error

JavaScript variables var let const JavaScript declarations