{"id":25606976,"url":"https://github.com/chsdwn/swift-for-javascript-developers","last_synced_at":"2025-10-27T10:37:34.805Z","repository":{"id":235175097,"uuid":"656385940","full_name":"chsdwn/swift-for-javascript-developers","owner":"chsdwn","description":"Learn Swift by comparing it with JavaScript examples.","archived":false,"fork":false,"pushed_at":"2023-06-23T16:07:55.000Z","size":7,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-14T20:16:37.029Z","etag":null,"topics":["comparison","example","javascript","swift"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/chsdwn.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2023-06-20T21:09:01.000Z","updated_at":"2024-12-17T00:33:13.000Z","dependencies_parsed_at":"2024-04-22T15:06:02.211Z","dependency_job_id":"50504d90-2274-436c-ba45-b992d9dcae5c","html_url":"https://github.com/chsdwn/swift-for-javascript-developers","commit_stats":null,"previous_names":["chsdwn/swift-for-javascript-developers"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/chsdwn/swift-for-javascript-developers","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chsdwn%2Fswift-for-javascript-developers","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chsdwn%2Fswift-for-javascript-developers/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chsdwn%2Fswift-for-javascript-developers/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chsdwn%2Fswift-for-javascript-developers/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chsdwn","download_url":"https://codeload.github.com/chsdwn/swift-for-javascript-developers/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chsdwn%2Fswift-for-javascript-developers/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279021014,"owners_count":26086947,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-14T02:00:06.444Z","response_time":60,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["comparison","example","javascript","swift"],"created_at":"2025-02-21T19:17:53.932Z","updated_at":"2025-10-14T20:16:37.849Z","avatar_url":"https://github.com/chsdwn.png","language":null,"readme":"# Swift for JavaScript Developers\n\n## Variables\n\n```js\nlet num = 1\nconsole.log(num) // 1\nnum = 2\nconsole.log(num) // 2\n```\n```swift\nvar num = 1\nprint(num) // 1\nnum = 2\nprint(num) // 2\n```\n\n### Constants\n\n```js\nconst num = 1\nconsole.log(num) // 1\n```\n```swift\nlet num = 1\nprint(num) // 1\n```\n\n## Strings\n\n```js\nconst name = \"Ali\"\nconst signature = `\n  Signed by ${name}\n`\nconsole.log(name.length) // 3\nconsole.log(name.toUpperCase()) // \"ALI\"\nconsole.log(name.startsWith(\"Al\")) // true\nconsole.log(name.endsWith(\"li\")) // true\n\nconsole.log(\"x + \" + 1) // \"x + 1\"\nconsole.log(`2 + 2 = ${2 + 2}`) // \"2 + 2 = 4\"\n```\n```swift\nlet name = \"Ali\"\nlet signature = \"\"\"\n  Signed by \\(name)\n\"\"\"\nprint(name.count) // 3\nprint(name.uppercased()) // \"ALI\"\nprint(name.hasPrefix(\"Al\")) // true\nprint(name.hasSuffix(\"li\")) // true\n\nprint(\"x + \" + String(1)) // x + 1\nprint(\"2 + 2 = \\(2 + 2)\") // 2 + 2 = 4\n```\n\n## Numbers\n\n```js\nlet num = 1_000\nnum += 1\nconsole.log(num) // 1001\n\nNumber.prototype.isMultiple = function({ of = 1 }) {\n  return this % of === 0\n}\nconsole.log(num.isMultiple({ of: 2 })) // false\nconsole.log(4..isMultiple({ of: 2 })) // true\n```\n```swift\nvar num = 1_000\nnum += 1\nprint(num) // 1001\nprint(num.isMultiple(of: 2)) // false\nprint(4.isMultiple(of: 2)) // true\n```\n\n### Decimal Numbers\n\n```js\nconst num = 0.1 + 0.2\nconsole.log(num) // 0.30000000000000004\n\nlet x = 1.5\nconst y = 2\nconsole.log(x + y) // 3.5\nconsole.log(Math.floor(x) + y) // 3\n\nx *= 2\nconsole.log(x) // 3\n```\n```swift\nlet num = 0.1 + 0.2\nprint(num) // 0.30000000000000004\n\nvar x = 1.5\nlet y = 2\nprint(x + Double(y)) // 3.5\nprint(Int(x) + y) // 3\n\nx *= 2\nprint(x) // 3.0\n```\n\n## Booleans\n\n```js\nlet isAdmin = false\nisAdmin = !isAdmin\nconsole.log(isAdmin) // true\n```\n```swift\nvar isAdmin = false\nisAdmin = !isAdmin\nprint(isAdmin) // true\n\nisAdmin.toggle()\nprint(isAdmin) // false\n```\n\n## Arrays\n\n```js\nconst numbers = [1, 2, 3]\nnumbers.push(4)\n\nconst names = new Array()\nnames.push(\"Ali\")\nconsole.log(names[0]) // \"Ali\"\n\nconst grades = []\ngrades.push(73)\n\nlet alphabet = [\"a\", \"b\", \"c\", \"d\"]\nconsole.log(alphabet.length) // 4\nconsole.log(alphabet.splice(2, 1)) // [\"c\"]\nalphabet.length = 0\n\nalphabet = [\"c\", \"a\", \"d\", \"b\"]\nconsole.log(alphabet.includes(\"e\")) // false\nconsole.log([...alphabet].sort()) // [\"a\", \"b\", \"c\", \"d\"]\nconsole.log([...alphabet].reverse()) // [\"b\", \"d\", \"a\", \"c\"]\n```\n```swift\nvar numbers = [1, 2, 3]\nnumbers.append(4)\n\nvar names = Array\u003cString\u003e()\nnames.append(\"Ali\")\nprint(names[0]) // \"Ali\"\n\nvar grades = [Int]()\ngrades.append(73)\n\nvar alphabet: [String] = [\"a\", \"b\", \"c\", \"d\"]\nprint(alphabet.count) // 4\nprint(alphabet.remove(at: 2)) // \"c\"\nalphabet.removeAll()\n\nalphabet = [\"c\", \"a\", \"d\", \"b\"]\nprint(alphabet.contains(\"e\")) // false\nprint(alphabet.sorted()) // [\"a\", \"b\", \"c\", \"d\"]\nprint(alphabet.reversed()) // ReversedCollection\u003cArray\u003cString\u003e\u003e(_base: [\"c\", \"a\", \"d\", \"b\"])\n```\n\n## Dictionaries\n\n```js\nconst ali = {\n  name: \"Ali\",\n  surname: \"Veli\",\n  age: \"20\",\n}\nconsole.log(ali.name) // \"Ali\"\nconsole.log(ali.surname) // \"Veli\"\nconsole.log(ali.age) // \"20\"\n\nconst olympics = new Map()\nolympics.set(2012, \"London\")\nolympics.set(2016, \"Rio\")\nolympics.set(2016, \"Rio de Janeiro\")\nolympics.set(2021, \"Tokyo\")\nconsole.log(olympics.get(2012)) // \"London\"\nconsole.log(olympics.size) // 3\n```\n```swift\nlet ali = [\n  \"name\": \"Ali\",\n  \"surname\": \"Veli\",\n  \"age\": \"20\",\n]\nprint(ali[\"name\", default: \"Unknown\"]) // Optional(\"Ali\")\nprint(ali[\"surname\", default: \"Unknown\"]) // Optional(\"Veli\")\nprint(ali[\"age\", default: \"Unknown\"]) // Optional(\"20\")\n\nvar olympics = [Int: String]()\nolympics[2012] = \"London\"\nolympics[2016] = \"Rio\"\nolympics[2016] = \"Rio de Janeiro\"\nolympics[2021] = \"Tokyo\"\nprint(olympics[2012, default: \"World\"]) // Optional(\"London\")\nprint(olympics.count) // 3\n```\n\n## Sets\n\n```js\nconst names = new Set([\"Ali\", \"Veli\", \"Ahmet\"])\nconsole.log(names) // Set { \"Ali\", \"Veli\", \"Ahmet\" }\n\nconst cities = new Set()\ncities.add(\"İstanbul\")\ncities.add(\"İstanbul\")\nconsole.log(cities.has(\"İstanbul\")) // true\nconsole.log(cities.size) // 1\ncities.add(\"Ankara\")\nconsole.log(Array.from(cities).sort()) // [\"Ankara\", \"İstanbul\"]\n```\n```swift\nlet names = Set([\"Ali\", \"Veli\", \"Ahmet\"])\nprint(names) // [\"Ali\", \"Veli\", \"Ahmet\"]\n\nvar cities = Set\u003cString\u003e()\ncities.insert(\"İstanbul\")\ncities.insert(\"İstanbul\")\nprint(cities.contains(\"İstanbul\")) // true\nprint(cities.count) // 1\ncities.insert(\"Ankara\")\nprint(cities.sorted()) // [\"Ankara\", \"İstanbul\"]\n```\n\n## Enums\n\n```js\nconst Weekday = Object.freeze({\n  monday: Symbol(\"monday\"),\n  tuesday: Symbol(\"tuesday\"),\n  wednesday: Symbol(\"wednesday\"),\n  thursday: Symbol(\"thursday\"),\n  friday: Symbol(\"friday\"),\n})\n```\n```swift\nenum Weekday {\n  case monday\n  case tuesday\n  case wednesday\n  case thursday\n  case friday\n}\nvar day = Weekday.monday\nday = Weekday.tuesday\nday = .wednesday\n\nenum Weekend {\n  case saturday, sunday\n}\n```\n\n## Type Annonations\n\n```ts\nconst name: string = \"Ali\"\nconst score: number = 0\nconst pi: number = 3.141\nconst isAdmin: boolean = true\nconst albums: string[] = [\"Century Child\", \"Human. :II: Nature.\"]\nconst olympics: Map\u003cnumber, string\u003e = new Map([\n  [2012, \"London\"],\n  [2016, \"Rio\"],\n  [2021, \"Tokyo\"],\n])\nconst books: Set\u003cstring\u003e = new Set([\"The Name of the Wind\", \"The Wise Man's Fear\"])\n```\n```swift\nlet name: String = \"Ali\"\nlet score: Double = 0\nlet pi: Double = 3.141\nlet isAdmin: Bool = true\nlet albums: [String] = [\"Century Child\", \"Human. :II: Nature.\"]\nlet olympics: [Int: String] = [2012: \"London\", 2016: \"Rio\", 2021: \"Tokyo\"]\nlet books: Set\u003cString\u003e = Set([\"The Name of the Wind\", \"The Wise Man's Fear\"])\n```\n\n## Conditions\n\n### `if`/`else`\n\n```js\nconst age = 18\nconst isAdmin = true\nif (age \u003e= 18 \u0026\u0026 isAdmin) {\n  console.log(\"You're authenticated\")\n} else if (age \u003c 18) {\n  console.log(\"You're not an adult\")\n} else if (!isAdmin) {\n  console.log(\"Contact with an admin\")\n} else {\n  console.log(\"You're unauthenticated\")\n}\n```\n```swift\nlet age = 18\nlet isAdmin = true\nif age \u003e= 18 \u0026\u0026 isAdmin {\n  print(\"You're authenticated\")\n} else if age \u003c 18 {\n  print(\"You're not an adult\")\n} else if !isAdmin {\n  print(\"Contact with an admin\")\n} else {\n  print(\"You're unauthenticated\")\n}\n```\n\n### `switch`/`case`\n\n```js\nconst num = 2\nswitch (num) {\n  case 1:\n    console.log(\"one\")\n    break\n  case 2:\n    console.log(\"two\") // \"two\"\n  case 3:\n    console.log(\"three\") // \"three\"\n  default:\n    console.log(\"4,5,6,...\") // \"4,5,6,...\"\n}\n```\n```swift\nlet num = 2\nswitch num {\ncase 1:\n  print(\"one\")\ncase 2:\n  print(\"two\") // \"two\"\n  fallthrough\ncase 3:\n  print(\"three\") // \"three\"\n  fallthrough\ndefault:\n  print(\"4,5,6,...\") // \"4,5,6,...\"\n}\n```\n\n### Ternary Conditional Operator: `?:`\n\n```js\nconst age = 18\nconst canVote = age \u003e= 18 ? \"Y\" : \"N\"\nconsole.log(canVote) // \"Y\"\n```\n```swift\nlet age = 18\nlet canVote = age \u003e= 18 ? \"Y\" : \"N\"\nprint(canVote) // \"Y\"\n```\n\n## Loops\n\n### `for` Loop\n\n```js\nconst alphabet = [\"a\", \"b\", \"c\"]\nfor (const char of alphabet) {\n  console.log(char) // \"a\", \"b\", \"c\"\n}\n\nconst range = (from, to, { inclusive = true } = {}) =\u003e \n  Array(to - from + Number(inclusive)).fill(null).map((_, i) =\u003e i + from)\n// range(3, 5) = 3...5\nfor (const num of range(3, 5)) {\n  if (num === 5) break\n  if (num % 2 === 1) continue\n  console.log(num) // 2, 4\n}\n// range(3, 5, { inclusive: false }) = 3..\u003c5\nfor (const num of range(3, 5, { inclusive: false })) {\n  console.log(num) // 3, 4\n}\n```\n```swift\nlet alphabet = [\"a\", \"b\", \"c\"]\nfor char in alphabet {\n  print(char) // \"a\", \"b\", \"c\"\n}\n\nfor num in 1...10 {\n  if num == 5 {\n    break\n  }\n  if num % 2 == 1 {\n    continue\n  }\n  print(num) // 2, 4\n}\nfor num in 3..\u003c5 {\n  print(num) // 3, 4\n}\n```\n\n### `while` Loop\n\n```js\nlet num = 0\nwhile (num !== 4) {\n  num = Math.ceil(Math.random() * 10)\n  console.log(num) // 1 to 10\n}\n```\n```swift\nvar num = 0\nwhile num != 4 {\n  num = Int.random(in: 1...10)\n  print(num) // 1 to 10\n}\n```\n\n## Functions\n\n```js\nconst isEven = ({ num }) =\u003e {\n  const res = num % 2 === 0\n  return res\n}\nconsole.log(isEven({ num: 3 })) // false\n```\n```swift\nfunc isEven(num: Int) -\u003e Bool {\n  let res = num % 2 == 0\n  return res\n}\nprint(isEven(num: 3)) // false\n```\n\n### Return Multiple Values\n\n```js\nconst getUserArray = () =\u003e [\"Floor\", \"Jansen\"]\nconst userArray = getUserArray()\nconsole.log(`${userArray[0]} ${userArray[1]}`) // \"Floor Jansen\"\n\nconst getUserDict = () =\u003e ({ name: \"Floor\", surname: \"Jansen\" })\nconst userDict = getUserDict()\nconsole.log(`${userDict.name} ${userDict.surname}`) // \"Floor Jansen\"\n\nconst getUserTuple = () =\u003e [\"Floor\", \"Jansen\"]\nconst userTuple = getUserTuple()\nconsole.log(`${userArray[0]} ${userArray[1]}`) // \"Floor Jansen\"\n\nconst getUserNamedTuple = () =\u003e ({ name: \"Floor\", surname: \"Jansen\" })\nconst { name, surname } = getUserNamedTuple()\nconsole.log(`${name} ${surname}`) // \"Floor Jansen\"\n```\n```swift\nfunc getUserArray() -\u003e [String] {\n  [\"Floor\", \"Jansen\"]\n}\nlet userArray = getUserArray()\nprint(\"\\(userArray[0]) \\(userArray[1])\") // \"Floor Jansen\"\n\nfunc getUserDict() -\u003e [String: String] {\n  [\"name\": \"Floor\", \"surname\": \"Jansen\"]\n}\nlet userDict = getUserDict()\nprint(\"\\(userDict[\"name\", default: \"Ali\"]) \\(userDict[\"surname\", default: \"Veli\"])\") // \"Floor Jansen\n\nfunc getUserTuple() -\u003e (String, String) {\n  (\"Floor\", \"Jansen\")\n}\nlet userTuple = getUserTuple()\nprint(\"\\(userTuple.0) \\(userTuple.1)\") // \"Floor Jansen\"\n\nfunc getUserNamedTuple() -\u003e (name: String, surname: String) {\n  // (\"Floor\", \"Jansen\") // This is valid also\n  (name: \"Floor\", surname: \"Jansen\")\n}\nlet (name, _) = getUserNamedTuple()\nlet (_, surname) = getUserNamedTuple()\nprint(\"\\(name) \\(surname)\") // \"Floor Jansen\"\n```\n\n### Customize Parameter Labels\n\n```js\nconst isOdd = (num) =\u003e {\n  return num % 2 === 1\n}\nconsole.log(isOdd(3)) // true\n\nconst printTimesTable = ({ for: num }) =\u003e {\n  for (let i = 1; i \u003c= 3; i++) {\n    console.log(`${i} x ${num} = ${i * num}`)\n  }\n}\nprintTimesTable({ for: 5 }) // \"1 x 5 = 5\", \"2 x 5 = 10\", \"3 x 5 = 15\"\n```\n```swift\nfunc isOdd(_ num: Int) -\u003e Bool {\n  num % 2 == 1\n}\nprint(isOdd(3)) // true\n\nfunc printTimesTable(for num: Int) {\n  for i in 1...3 {\n    print(\"\\(i) x \\(num) = \\(i * num)\")\n  }\n}\nprintTimesTable(for: 5) // \"1 x 5 = 5\", \"2 x 5 = 10\", \"3 x 5 = 15\"\n```\n\n### Default Values for Parameters\n\n```js\nconst printNumbers = ({ from, to = 10 }) =\u003e {\n  for (let i = from; i \u003c= to; i++) console.log(i)\n}\nprintNumbers({ from: 8 }) // 8, 9, 10\n```\n```swift\nfunc printNumbers(from: Int, to: Int = 10) {\n  for i in from...to {\n    print(i)\n  }\n}\nprintNumbers(from: 8) // 8, 9, 10\n```\n\n### Handle Errors in Functions\n\n```js\nconst PasswordError = Object.freeze({\n  short: Symbol(\"short\"),\n  obvious: Symbol(\"obvious\"),\n})\n\nconst checkPassword = (password) =\u003e {\n  const length = password.length\n  if (length \u003c 5) throw PasswordError.short\n  if (password === \"12345\") throw PasswordError.obvious\n\n  if (length \u003c 8) return \"Ok\"\n  if (length \u003c 10) return \"Good\"\n  return \"Excellent\"\n}\n\ntry {\n  const res = checkPassword(\"12345\")\n  console.log(res)\n} catch (err) {\n  if (err === PasswordError.short) {\n    console.log(\"Short password\")\n  } else if (err === PasswordError.obvious) {\n    console.log(\"Try a stronger password.\") // \"Try a stronger password.\"\n  } else {\n    console.log(\"An error occured.\")\n  }\n}\n```\n```swift\nenum PasswordError: Error {\n  case short, obvious\n}\n\nfunc checkPassword(_ password: String) throws -\u003e String {\n  let count = password.count\n  if count \u003c 5 {\n    throw PasswordError.short\n  }\n\n  if password == \"12345\" {\n    throw PasswordError.obvious\n  }\n\n  if count \u003c 8 {\n    return \"Ok\"\n  } \n  if count \u003c 10 {\n    return \"Good\"\n  }\n  return \"Excellent\"\n}\n\ndo {\n  let res = try checkPassword(\"12345\")\n  print(res)\n} catch PasswordError.short {\n  print(\"Short password\")\n} catch PasswordError.obvious {\n  print(\"Try a stronger password.\") // \"Try a stronger password.\"\n} catch {\n  print(\"An error occured\")\n}\n```\n\n## Closures\n\n```js\nconst sayHi = () =\u003e console.log(\"Hi!\")\nsayHi() // \"Hi\"\n\nconst greet = (name) =\u003e `Hi, ${name}!`\nconsole.log(greet(\"Ali\")) // \"Hi, Ali!\"\n\nconst sorted = [3, 1, 2].sort((a, b) =\u003e a - b)\nconsole.log(sorted) // [1, 2, 3]\n```\n```swift\nlet sayHi = {\n  print(\"Hi!\")\n}\nsayHi() // \"Hi!\"\n\nlet greet = { (name: String) -\u003e String in\n  \"Hi, \\(name)\"\n}\nprint(greet(\"Ali\")) // \"Hi, Ali!\"\n\nlet sorted = [3, 1, 2].sorted(by: { (a: Int, b: Int) -\u003e Bool in\n  a \u003c b\n})\nprint(sorted) // [1, 2, 3]\n```\n\n### Trailing Closure\n\n```js\nconst sorted = [3, 1, 2].sort((a, b) =\u003e a - b)\nconsole.log(sorted) // [1, 2, 3]\n\nconst reversed = [3, 1, 2].sort((a, b) =\u003e b - a)\nconsole.log(reversed) // [3, 2, 1]\n```\n```swift\nlet sorted = [3, 1, 2].sorted { a, b in \n  a \u003c b\n}\nprint(sorted) // [1, 2, 3]\n\nlet reversed = [3, 1, 2].sorted { $0 \u003e $1 }\nprint(reversed) // [3, 2, 1]\n```\n\n### Functions as Parameters\n\n```js\nconst makeArray = ({ size, using: generator }) =\u003e {\n  const numbers = []\n  for (let i = 0; i \u003c size; i++) {\n    numbers.push(generator())\n  }\n  return numbers;\n}\n```\n```swift\nfunc makeArray(size: Int, using generator: () -\u003e Int) -\u003e [Int] {\n  var numbers = [Int]()\n  for _ in 0..\u003csize {\n    numbers.append(generator())\n  }\n  return numbers\n}\n```\n\n#### Example 1\n\n```js\nconst arr = makeArray({ size: 3, using: () =\u003e Math.floor(Math.random() * 10) })\nconsole.log(arr) // [4, 2, 8]\n```\n```swift\nlet arr = makeArray(size: 3) { Int.random(in: 1..\u003c10) }\nprint(arr) // [8, 6, 2]\n```\n\n#### Example 2\n\n```js\nconst generateNumber = () =\u003e Math.floor(Math.random() * 10)\nconst arr = makeArray({ size: 3, using: generateNumber })\nconsole.log(arr) // [5, 0, 4]\n```\n```swift\nfunc generateNumber() -\u003e Int { Int.random(in: 0..\u003c10 )}\nlet arr = makeArray(size: 3, using: generateNumber)\nprint(arr) // [7, 9, 3]\n```\n\n#### Example 3\n\n```js\nconst printThreeSteps = ({ first, second, third }) =\u003e {\n  console.log(\"1st:\")\n  first()\n  console.log(\"2nd:\")\n  second()\n  console.log(\"3rd:\")\n  third()\n  console.log(\"Completed\")\n}\nprintThreeSteps({\n  first: () =\u003e console.log(\"a\"),\n  second: () =\u003e console.log(\"b\"),\n  third: () =\u003e console.log(\"c\"),\n}) // \"1st:\", \"a\", \"2nd:\", \"b\", \"3rd:\", \"c\", \"Completed\"\n```\n```swift\nfunc printThreeSteps(first: () -\u003e Void, second: () -\u003e Void, third: () -\u003e Void) {\n  print(\"1st:\")\n  first()\n  print(\"2nd:\")\n  second()\n  print(\"3rd:\")\n  third()\n  print(\"Completed\")\n}\nprintThreeSteps {\n  print(\"a\")\n} second: {\n  print(\"b\")\n} third: {\n  print(\"c\")\n} // \"1st:\", \"a\", \"2nd:\", \"b\", \"3rd:\", \"c\", \"Completed\"\n```\n\n### How to Chain Functions\n\n```js\nconst arr = [4, 3, 2, 5, 1]\n  .filter(x =\u003e x % 2 === 0)\n  .map(x =\u003e x * x)\n  .sort((x, y) =\u003e x - y)\nconsole.log(arr) // [4, 16]\n```\n```swift\nlet arr = [4, 3, 2, 5, 1].filter { \n  $0 % 2 == 0\n}.map {\n  $0 * $0\n}.sorted {\n  $0 \u003c $1\n}\nprint(arr) // [4, 16]\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchsdwn%2Fswift-for-javascript-developers","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchsdwn%2Fswift-for-javascript-developers","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchsdwn%2Fswift-for-javascript-developers/lists"}