PY
JS

Python to JavaScript

11 lessons

Progress0%
1Variables and Constants2Functions and Closures3Lists vs Arrays4Dicts vs Objects and Map5Classes and Prototypes6Modules and Imports7Async Programming8DOM and Browser APIs9Type Hints vs TypeScript10Error Handling11Build Tools and npm
All Mirror Courses
PY
JS
Variables and Constants
MirrorLesson 1 of 11
Lesson 1

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.

Mirror Card
PY
From Python:

In Python, you're familiar with declaring and using variables.

JS
In JavaScript:

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:

JS
JavaScript 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:

PY
Python (What you know)
# Variables
name = "Alice"
name = "Bob"

# Constants (convention)
PI = 3.14159

# Multiple assignment
x, y, z = 1, 2, 3
Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

JS
In JavaScript:

JavaScript requires let or const keywords

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

JS
In JavaScript:

const creates true constants

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

JS
In JavaScript:

Booleans are lowercase: true/false

Step-by-Step Breakdown

1. Variable Keywords

JavaScript requires keywords. Use let for variables, const for constants.

PY
Python
name = "Alice"
JS
JavaScript
let name = "Alice";
Rule of Thumb
Prefer const by default. Use let only when you need to reassign.

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
Common Pitfall
Don't assume JavaScript works exactly like Python. While the concepts may be similar, the syntax and behavior can differ significantly.

Key Takeaways

  • let for variables, const for constants
  • Semicolons are conventional
  • Booleans are lowercase
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your Python code in JavaScript to practice these concepts.
OverviewNext