Python to JavaScript Translation Guide ### Definitions - **Statement**: A single instruction in JavaScript. - **Sequence**: Linear sequence of statements, same as in Python. - **Control**: Conditional execution using `if`. - **Loop**: Repeating statements using `for` or `while`. - **Function**: Block of code defined using `function`. - **Recursion**: Function that calls itself. ### Data Types and Variables - **Literal**: Fixed values like `1`, `true`, `"hello"`. - **Variable**: Assigned via `=` (e.g., `let x = 10;`). - **Global variable**: Available in global scope. ### Operators - **Arithmetic operators**: `+`, `-`, `*`, `/`, `%`, `**` - **Logic operators**: `&&`, `||`, `!` - **Comparison operators**: `==`, `===`, `!=`, `!==`, `>`, `<`, `>=`, `<=` ### Control Structures - **If-Else**: `if`, `else if`, `else` - **Loops**: `for`, `while`, `do...while`, `break`, `continue` - **Try-Catch**: Exception handling with `try`, `catch`, `finally` - **Function Definitions**: Using `function` keyword or arrow functions `() => {}` ### Control of Data - **Mutable vs Immutable**: Objects and arrays are mutable; primitives like numbers and strings are immut - **Namespaces**: JavaScript uses scope to manage variable access. ### Iterables and Iteration - **For loop**: `for (let i = 0; i < arr.length; i++) { ... }` - **For...of loop**: `for (let element of arr) { ... }` - **While loop**: `while (condition) { ... }` ### Iterators - **Iterator protocol**: Objects implementing the `next()` method. - **Iterable**: Objects implementing the `[Symbol.iterator]()` method. ```javascript let arr = [1, 2, 3]; let iterator = arr[Symbol.iterator](); console.log(iterator.next().value); // 1 ``` ### Higher-order Functions - **map, filter, reduce** ```javascript let numbers = [1, 2, 3, 4, 5]; let doubled = numbers.map(x => x * 2); // [2, 4, 6, 8, 10] let evens = numbers.filter(x => x % 2 === 0); // [2, 4] let sum = numbers.reduce((acc, curr) => acc + curr, 0); // 15 ``` ### Strings - **Concatenation**: `"Hello " + "World"` - **Template Literals**: `` `Hello ${name}` `` - **String Methods**: `str.toUpperCase()`, `str.includes()` ### Lists (Arrays) - **Mutation**: `arr.push()`, `arr.pop()`, `arr.sort()` - **Iteration**: `arr.forEach()` ### Dictionaries (Objects) - **Access**: `obj.key`, `obj['key']` - **Methods**: `Object.keys(obj)`, `Object.values(obj)`, `Object.entries(obj)` ### Sets - **Creation**: `let mySet = new Set([1, 2, 3]);` - **Methods**: `mySet.add(4)`, `mySet.has(2)`, `mySet.delete(3)` ### Errors - **Try-Catch**: ```javascript try { // code that may throw an error } catch (error) { // code to handle the error } finally { // code to run regardless of error } ``` ### Modules - **Importing and Exporting** ```javascript // In module.js export function greet() { console.log("Hello, World!"); } // In another file import { greet } from './module.js'; greet(); // Hello, World! ``` ### JSON - **Parse**: `JSON.parse()` - **Stringify**: `JSON.stringify()` ### File Handling - **Reading and Writing Files**: Typically done using Node.js in a server environment. ### Example Translations #### If-Else Python: ```python if condition: # code elif condition: # code else: # code ``` JavaScript: ```javascript if (condition) { // code } else if (condition) { // code } else { // code } ``` #### Functions Python: ```python def add(a, b): return a + b ``` JavaScript: ```javascript function add(a, b) { return a + b; } // or using arrow function const add = (a, b) => a + b; ``` #### For Loop Python: ```python for i in range(5): print(i) ``` JavaScript: ```javascript for (let i = 0; i < 5; i++) { console.log(i); } ```