This story on HackerNoon has a decentralized backup on Sia.
Transaction ID: sg_8w6zjvuEY1vWQCZg_Vu8rWNrHRXcckbB3Sr98Sv4
Cover

Swift and JavaScript comparison snippets(1) - The Basics

Written by @unbug | Published on 2018/6/27

TL;DR
Swift

Constants and Variables

Swift

// declare a constantlet maximumNumberOfLoginAttempts = 10

// declare a variablevar currentLoginAttempt = 0

// declare multiple constants or multiple variables on a single line, separated by commasvar x = 0.0, y = 0.0, z = 0.0

JavaScript

// declare a constantconst maximumNumberOfLoginAttempts = 10

// declare a variablevar currentLoginAttempt = 0// orlet currentLoginAttempt = 0

// declare multiple constants or multiple variables on a single line, separated by commasvar x = 0.0, y = 0.0, z = 0.0

Comments

Swift

// This is a comment.

/* This is also a commentbut is written over multiple lines. */

JavaScript

// This is a comment.

/* This is also a commentbut is written over multiple lines. */

Numeric Type Conversion

Swift

let pi = 3.14159// Integer and Floating-Point Conversionlet integerPi = Int(pi)

JavaScript

const pi = 3.14159// Integer and Floating-Point Conversionconst integerPi = parseInt(pi)

Booleans

Swift

let orangesAreOrange = truelet turnipsAreDelicious = false

if turnipsAreDelicious {print("Mmm, tasty turnips!")} else {print("Eww, turnips are horrible.")}

JavaScript

const orangesAreOrange = trueconst turnipsAreDelicious = false

if (turnipsAreDelicious) {console.log("Mmm, tasty turnips!")} else {console.log("Eww, turnips are horrible.")}

Error Handling

Swift

func canThrowAnError() throws {// this function may or may not throw an error}do {try canThrowAnError()// no error was thrown} catch {// an error was thrown}

JavaScript

function canThrowAnError() {// this function may or may not throw an error}try {canThrowAnError()// no error was thrown} catch (e) {// an error was thrown}

More of Swift and JavaScript comparison snippets

[story continues]


Written by
@unbug
Senior Software Engineer

Topics and
tags
javascript|swift|swift-vs-javascript|swift-and-javascript
This story on HackerNoon has a decentralized backup on Sia.
Transaction ID: sg_8w6zjvuEY1vWQCZg_Vu8rWNrHRXcckbB3Sr98Sv4