JavaScript variables are used to store and manage data in a program. They act as containers that hold values, which can be changed or reused throughout the script.
In JavaScript, variables are declared using var, let, or const.
var (Old Way, Avoid if Possible)Example:
var name = "John";
console.log(name); // Output: John
let (Recommended){}).Example:
let age = 25;
age = 26; // Allowed (Updating)
console.log(age); // Output: 26
const (For Fixed Values)let.Example:
const PI = 3.1416;
// PI = 3.14; // ❌ Error! Cannot change a const value
console.log(PI); // Output: 3.1416
✔ Can contain letters, numbers, underscores (_), and dollar signs ($).
✔ Must not start with a number.
✔ Case-sensitive (myVar and myvar are different).
✔ Cannot use JavaScript reserved words (like let, function, return).
Valid Examples:
let userName = "Alice";
let $price = 100;
let _totalAmount = 500;
Invalid Examples: