full_name = "Jane Hacker" let fullName = "Jane Hacker"; let {ola, oi} = translation; pi = 3.14 const pi = 3.14; // ...and the reverse const age = 3; # lists ("Arrays" in JS) // Arrays ("lists" in Py) let info = {fullName, age}; names = ["John", "Paul", "G"] let names = ["John", "Paul", "G"]; // Combine obj, arrays with splat # dicts (similar to "Objects") // Objects (similar to "dicts") const addedTranslations = { translation = { let translation = { ...translation, "ola": "Hello", ola: "Hello", gato: "cat", "oi": "hi", oi: "Hi", }; } }; const extra = [...names, "Mary"];
# For loops // For loop
for name in names: for (let name of names) { Legacy Syntax print("name:", name) console.log("name:", name); } # While loops // While loops // Loop through properties x = 0 let x = 0; for (var i in arr) {} while x < 3: while (x < 3) { // C-style: Loop through numbers print("X:", x) console.log("X:", x); for (var i = 0; i < 100; i++) {} x += 1 x++; var a = 3; // function-scoped } a = 3; // globally scoped # If-statements // If-statements if full_name == "Jane": if (fullName === "Jane") { print("Hi, Jane!") console.log("Hi, Jane!"); elif full_name == "Alice": Async & Callbacks } else if (fullName === "Alice") { print("Hey Alice") console.log("Hey Alice"); else: } else { // Callbacks (common in node.js) print("Don’t know you") console.log("Don’t know you"); fs.readFile("file.txt", } (err, data) => { # List comprehension if (err) throw err; long_names = [ // Array processing (map & filter) console.log(data); name.upper() for name in names let longNames = names if len(name) > 3 .filter(n => n.length > 3) }); ] .map(n => n.toUpperCase()); // Promise (common in browser) # Functions // Functions fetch("http://site.com/api.json") def greeter(name): function greeter(name) { .then(response => response.json()) print("Hi", name) console.log("Hi", name); .then(data => { greeter("Bob") } console.log("Resp:", data); greeter("Bob"); }) # Lambda function .catch(err => { dst = lambda x, y: x*x + y*y // Arrow function expression console.log("error", err); const dst = (x, y) => x*x + y*y; }); # Conjunctions // Conjunctions if age < 18 and drink == "beer": if (age < 18 && drink === "beer") { asynchronous Instead of pausing print("Too young kiddo") console.log("Too young kiddo"); (“blocking”) for a slow operation, } the asynchronous approach is to if age > 18 or drink == "soda": if (age > 18 || drink === "soda") { put-off starting the slow operation, print("Great choice") console.log("Great choice"); then call a function (“callback”) } at a later time to signal it’s done # Class syntax // Class syntax callback A function passed as an argu- class User(BaseUser): ment to be called later when an class User extends BaseUser { event is triggered def __init__(self, name): constructor(name) { self.name = name promise Another popular way to do this.name = name; callbacks, with a .then syntax self.logged_in = False this.loggedIn = false; def log_in(self): } self.logged_in = True logIn() { Variable Declaration this.loggedIn = true; } } let Declare a variable (block scoped) user = User("jqhacker") let user = new User("jqhacker"); const Like let, cannot be reassigned.
JavaScript (ES6) kickstartcoding.com A cheatsheet by Kickstart Coding