JS Variables
Syntax
Variables in JavaScript are declared using let, const, or var to store data for use in your program.
let variableName; // Declares a variablevariableName = "value"; // Assigns a value
let newVariable = 42; // Declares AND assigns a variable
var oldVariable = true; // Declares a variable using 'var'Example
let age = 25; // Declares a variable to store ageconst PI = 3.14; // Declares a constant for the value of PI
var isStudent = false; // Declares a variable using 'var'
console.log(`Age: ${age}, PI: ${PI}, Student: ${isStudent}`);Best Practices
- Use
constby default unless you plan to reassign the variable, in which case uselet. - Avoid using
varto prevent unexpected behavior due to function-scoping and hoisting. - Use camelCase for variable names.