I’m learning Python!

Time to learn a new skill, here is my cheat sheet

JavaScript/TypeScript vs Python Crib Sheet

1. Variables and Data Types

Concept JavaScript / TypeScript Example Python Example
Declare variable (let, const) let x = 5; const y = 10; x = 5
Declare object let obj = { name: "John" } obj = { "name": "John" }
Declare array let arr = [1, 2, 3]; arr = [1, 2, 3]
Array of objects let arr = [{name: "A"}, {name: "B"}]; arr = [{"name": "A"}, {"name": "B"}]
Null null None
Undefined undefined None (used in place of undefined)
True/False true, false True, False

2. Objects vs Dictionaries

Concept JavaScript / TypeScript Python
Create object let obj = { key: "value" } obj = { "key": "value" }
Access object value obj.key or obj["key"] obj["key"]
Add/update key obj.newKey = "newValue" obj["newKey"] = "newValue"
Delete key delete obj.key del obj["key"]

3. Arrays vs Lists

Concept JavaScript / TypeScript Python
Create array/list let arr = [1, 2, 3]; arr = [1, 2, 3]
Access element arr[0] arr[0]
Add element arr.push(4) arr.append(4)
Remove element arr.pop() arr.pop()
Array length arr.length len(arr)
Loop through array arr.forEach(item => console.log(item)) for item in arr: print(item)

4. Spread Operator / Unpacking

Concept JavaScript / TypeScript Python
Spread array let newArr = [...arr, 4]; new_arr = [*arr, 4]
Spread object let newObj = {...obj, newKey: "new"} new_obj = {**obj, "newKey": "new"}
Spread function args function(...args) {} def function(*args):

5. Conditionals and Null/True Operations

Concept JavaScript / TypeScript Python
If statement if (condition) {} if condition:
Ternary let val = condition ? trueVal : falseVal val = trueVal if condition else falseVal
Nullish coalescing operator let val = x ?? "default"; val = x or "default"
Optional chaining let val = obj?.key; val = obj.get("key") or val = obj["key"] if obj else None

6. Loops

Concept JavaScript / TypeScript Python
For loop for (let i = 0; i < arr.length; i++) {} for i in range(len(arr)):
For…of loop (array) for (let item of arr) {} for item in arr:
For…in loop (object keys) for (let key in obj) {} for key in obj:
While loop while (condition) {} while condition:

7. Functions

Concept JavaScript / TypeScript Python
Declare function function myFunc() {} def my_func():
Arrow function const myFunc = () => {} N/A (use def my_func():)
Return return value; return value
Default parameter function myFunc(a = 10) {} def my_func(a=10):
Rest parameters function(...args) {} def function(*args):

8. Classes and Objects

Concept JavaScript / TypeScript Python
Declare class class MyClass {} class MyClass:
Constructor constructor() def __init__(self):
Instance method this.myMethod() self.my_method()
Create object let obj = new MyClass(); obj = MyClass()

9. Error Handling

Concept JavaScript / TypeScript Python
Try/Catch try { ... } catch (e) { ... } try: ... except Exception as e:

Key Differences to Keep in Mind:

  1. None vs. null/undefined: In Python, None represents both null and undefined in JavaScript. There’s no separate undefined in Python.
  2. Indentation matters: Python uses indentation to define blocks of code (no curly braces).
  3. No semi-colons: Python doesn’t require semi-colons at the end of statements.
  4. True/False: Use True and False in Python (capitalized), not true and false as in JavaScript.
  5. Method definition: When defining a class method, you need to include self as the first argument in Python, which refers to the instance of the class.

Leave a Reply