JavaScript's Variables
JavaScript Variables
Variables in JavaScript are used to store data. They can be declared using var, let, and const.
1. var
var was used in older versions of JavaScript. One of its problems is that it is function-scoped and does not support block-scoping.
var x = 10;
console.log(x); // 10
2. let
let was introduced in ES6. It supports block-scoping and is better than var.
let y = 20;
console.log(y); // 20
3. const
const is used for declaring constant variables. Once assigned, its value cannot be changed.
const z = 30;
console.log(z); // 30
Conclusion
It is important to use variables correctly in JavaScript. Avoid using var and prefer let or const. Use let if the value needs to change and const if it should remain constant.