JS Variables
What is JS Variables?
JavaScript variables are containers for storing data values. In modern JavaScript, you declare variables using let (for values that change) or const (for values that don't change). The older var keyword is still supported but less recommended.
Syntax & Usage
The syntax and usage patterns for JS Variables follow standard JavaScript conventions. Below you will see code examples that demonstrate the most common use cases. Try modifying these examples in your own editor to experiment with different approaches.
Example
Here is a practical example demonstrating JS Variables:
Tips & Best Practices
- Use
constby default,letwhen you need to reassign - Use strict equality (
===) instead of loose equality (==) - Handle errors with try/catch blocks for robust code
- Use template literals instead of string concatenation
Summary
In this lesson you learned about JS Variables. Practice writing code to reinforce these concepts and move on to the next lesson when you feel comfortable.
Examples
// Using let (can be reassigned)
let age = 25;
age = 26; // OK
// Using const (cannot be reassigned)
const name = 'Alice';
// name = 'Bob'; // Error!
// const with objects (properties can change)
const user = { name: 'Alice', age: 25 };
user.age = 26; // OK - modifying property
console.log(name, age); // Alice 26
Test Your Knowledge
Take a quick quiz to check what you've learned in this lesson.