Variables and Constants
Declaring and using variables
Introduction
In this lesson, you'll learn about variables and constants in JavaScript. Coming from Python, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.
In Python, you're familiar with declaring and using variables.
JavaScript has its own approach to declaring and using variables, which we'll explore step by step.
The JavaScript Way
Let's see how JavaScript handles this concept. Here's a typical example:
// Variables with let
let name = "Alice";
name = "Bob";
// True constants with const
const PI = 3.14159;
// Multiple declarations
let x = 1, y = 2, z = 3;Comparing to Python
Here's how you might have written similar code in Python:
# Variables
name = "Alice"
name = "Bob"
# Constants (convention)
PI = 3.14159
# Multiple assignment
x, y, z = 1, 2, 3You may be used to different syntax or behavior.
JavaScript requires let or const keywords
You may be used to different syntax or behavior.
const creates true constants
You may be used to different syntax or behavior.
Booleans are lowercase: true/false
Step-by-Step Breakdown
1. Variable Keywords
JavaScript requires keywords. Use let for variables, const for constants.
name = "Alice"let name = "Alice";Common Mistakes
When coming from Python, developers often make these mistakes:
- JavaScript requires let or const keywords
- const creates true constants
- Booleans are lowercase: true/false
Key Takeaways
- let for variables, const for constants
- Semicolons are conventional
- Booleans are lowercase