JavaScript Fundamentals
JavaScript basics - from function definition over data types to error handling
For Python developers, JavaScript can feel can feel quite strange when trying to read it. Mostly due to its extensive usage of brackets. But once fundamental differences are understood, JavaScript becomes familiar and as easy to write and read.
Syntax and Language Structure
Key Differences from Python
Python emphasizes readability through indentation. JavaScript, by contrast, uses braces {} to define blocks and semicolons ; to terminate statements.
⧉
1 2 | |
⧉
1 2 3 | |
Key distinctions: - Parentheses () are required around conditions - Braces {} define code blocks - Semicolons are technically optional but strongly recommended
Indentation in JavaScript is stylistic, not syntactic. Poor formatting won’t break execution, but it will absolutely hurt readability. That's why semicolons are recommended. A semicolon marks the end of a statement. Modern browsers know how to interpret neatly formatted code that uses newlines to terminate blocks. However obfuscated code, something like this:
⧉
1 | |
needs semicolons to make it readable to the browser so it can interpret end of blocks (like if-statements and for-loops) properly.
Variables: var, let, const
Python variables are created via assignment, with no explicit declaration:
⧉
1 | |
JavaScript requires declaration keywords:
⧉
1 2 3 | |
Differences: - let: block-scoped, mutable (most commonly used) - const: block-scoped, immutable binding (preferred by default) - var: function-scoped, legacy (avoid in modern code)
Important nuance:
⧉
1 2 | |
const prevents reassignment, not mutation of contents.
Naming Conventions and Idioms
Python uses snake_case:
⧉
1 | |
JavaScript uses camelCase:
⧉
1 | |
Conventions: - Variables/functions: camelCase - Classes: PascalCase - Constants: UPPER_CASE (less strictly enforced than Python)
Data Types and Structures
Primitive Types vs Python Equivalents
| JavaScript | Python Equivalent |
|---|---|
| number | int, float |
| string | str |
| boolean | bool |
| null | None |
| undefined | (no direct equivalent) |
Example:
⧉
1 2 3 4 5 | |
undefined means a variable exists but has no value—this concept doesn’t really exist in Python.
Objects vs Python Dictionaries
JavaScript objects are conceptually similar to Python dictionaries:
⧉
1 2 | |
⧉
1 2 | |
Differences: - Dot notation (user.name) is common in JavaScript - Keys are typically strings (even if not quoted)
The wording might be confusing since "object" means something entirely different in Python. Just remember that in JavaScript an object is a dictionary. Nothing more.
Arrays vs Python Lists
⧉
1 2 | |
⧉
1 2 | |
JavaScript arrays are more flexible but less consistent - they are technically objects (javascript dictionaries) and can behave unexpectedly.
Type Coercion (Critical Concept)
JavaScript performs implicit type conversions:
⧉
1 2 | |
Python would raise an error: TypeError: "5" + 1
To avoid surprises, use strict equality:
⧉
1 2 | |
Rule of thumb: Always prefer === over ==. === does not just compare values, it also compares the value types. Only if value AND type match, === returns true.
Functions and Scope
Function Declarations vs Expressions
Function declaration:
⧉
1 2 3 | |
Function expression:
⧉
1 2 3 | |
Difference: - Declarations are hoisted (can be used before definition) - Expressions are not
Arrow Functions
Arrow functions provide a concise syntax:
⧉
1 2 3 | |
Short form:
⧉
1 | |
When to use: - Short, functional-style code - Callbacks
When to avoid: - When you need a dynamic this (arrow functions don’t bind their own this)
Scope: Global, Function, Block
Global scope:
⧉
1 | |
Function scope:
⧉
1 2 3 | |
Block scope (let and const only):
⧉
1 2 3 4 | |
Important contrast:
⧉
1 2 3 4 5 | |
This is why var is discouraged and let or const are preferred.
Closures (Compared to Python)
Closures occur when a function retains access to its outer scope.
⧉
1 2 3 4 5 6 7 8 9 10 11 12 | |
⧉
1 2 3 4 5 6 7 8 9 10 | |
Key takeaway: JavaScript closures are more common and heavily used in callbacks and async patterns.
Control Flow
Conditionals
⧉
1 2 3 4 5 6 7 | |
Switch statement (Python -> match, case):
⧉
1 2 3 4 5 6 7 8 9 10 | |
Loops
Classic for loop:
⧉
1 2 3 | |
While loop:
⧉
1 2 3 4 5 | |
for...of (values) - iterating over i.e. arrays:
⧉
1 2 3 4 | |
for...in (keys):
⧉
1 2 3 4 | |
Python comparison: - for...of ≈ for x in list - for...in ≈ iterating over dict keys
Error Handling (try...catch)
⧉
1 2 3 4 5 6 7 | |
⧉
1 2 3 4 5 6 | |
JavaScript’s model is similar but less granular. There’s no built-in equivalent to multiple except types unless you manually inspect the error.
For Python developers, JavaScript’s fundamentals are less about learning entirely new concepts and more about recalibrating expectations. The syntax is different, but the deeper shift lies in: - Handling type coercion carefully - Understanding scope and closures - Adapting to JavaScript’s flexible (and sometimes inconsistent) structures
Once these are internalized, JavaScript becomes significantly more predictable and far more powerful as a complement to your Python skillset.
Join the Newsletter
Practical insights on Django, backend systems, deployment, architecture, and real-world development — delivered without noise.
Get updates when new guides, learning paths, cheat sheets, and field notes are published.
No spam. Unsubscribe anytime.
There is no third-party involved so don't worry - we won't share your details with anyone.