DEV Community

Paul Stumpe
Paul Stumpe

Posted on


JavaScript to Swift

Coming from JS to Swift the first thing you'll discover is that const is now let, and let is now var.
Javascript example

const one = 1;

Swift example

let one = 1;

And so here are the errors if we try to reassign them.

const one = 1;
one = 2;

Javascript error

Uncaught TypeError: Assignment to constant variable.

Swift example

let one = 1;
one = 2;

Swift error

error: cannot assign to value: 'one' is a 'let' constant
one = 2;

Unlike Javascript, Swift it a typed language. It will infer the type of the value you assign the first time to the best of its ability. But, if you attempt to later assign a new type to that same variable you're going to have problems. Because of its stricter typing rules, and a wider range of types compared to javascript, you have to consider what type of number you're intending to save, for instance. If you need the number to have decimals later, but are currently saving it as a whole number when you first declare your variable, you'll want to let Swift know that it really might be a decimal down the line. As such, you can actually distinguish what type this variable should be holding when you declare it. To do so you'll add a colon after the variable name and write the type directly after the colon before the equals operator or the value.

let one: Double = 1;
one = 1.1


let one = 1;
one = 1.1

The second example will error, as we didn't inform Swift in anyway that we needed a number that had decimal places so it defaulted to the type of integer. In JavaScript of course, this wasn't something we had to consider.

When using operators on different types in javascript, there is a lot of best tries at coercion. This isn't true in Swift. Here, we need to explicitly convert the type when writing the operation, like so:

let sentence = "The size is "
let size = 94
let fullSentence = sentence + String(size)

If you leave out the string declaration on the third, line you'll receive an error. Of course in javascript, the number would have simply been coerced.
Fortunately, Swift provides something extremely similar to template literals, that should be immediately comfortable when mixing string values with other values and variables. you'll use it by typing ().

let friends = 5
let enemies = 2
let friendsSentence = "I have \(friends) apples."
let friendsVsEnemies = "I have \(friends - enemies) more friends than enemies."

FriendsVsEnemies here would have a value of "I have 3 more friends than enemies."

I'll be going into more things of note between javascript and swift on my next blog, but one thing to remember if/when you're looking into Swift is that when people reference Dictionaries, they're the Swift equivalent of our objects. They also declare arrays and dictionaries both using [].

var friends = ["bob", "larry", "kyle", "lisa"]

this is an array.
And here is a dictionary version

var friend=["name": "Paul", "hobby":"coding"]

and accessing their values is just a simple bracket notation. friends[0] would give you "bob". To add to an array you can you its append method. calling a method will also be very familiar to JavaScript coders. It would simply be friends.append("Alex") I hope you enjoyed learning about Swift from a JS perspective!

Top comments (0)

You’ve already scrolled down this far, why not join our community of 900k+ developers all learning together?