JavaScript : Variables.
Table of contents
Variables are used to store values in JavaScript. They can be created using the var, let, or const keywords.
let, const and var
The var keyword is used to declare a variable that is scoped to the nearest function or block. The let keyword is similar to var, but it is block-scoped rather than function-scoped. The const keyword is used to declare a variable that cannot be reassigned.
Here's an example of how to declare and use variables in JavaScript:
let name = 'John';
console.log(name); // Output: 'John'
name = 'Jane';
console.log(name); // Output: 'Jane'
const PI = 3.14;
console.log(PI); // Output: 3.14
PI = 3.14159; // This will throw an error
It's important to choose the appropriate keyword for declaring variables based on how you plan to use them. For example, you might use const for variables that you don't want to change, and let for variables that you need to reassign.