Switching from JavaScript to Swift

Stone Fuglaar
5 min readApr 6, 2021

Most of the tutorials for the basics of Swift online are designed for people who are not used to developing. Here, I have gathered information and personal knowledge to explain how to easily view the language of Swift through the lenses of JavaScript.

Variables

Variables in Swift are more similar to JavaScript before ES6 was introduced. One of the main similarities is the broad use of “var.” Var behaves almost exactly like the “let” keyword from JavaScript in the sense that this is a variable that can be changed later in the code. When assigning a variable that you know you are going to change later, stick with var. The “let” keyword in Swift is the same as “const” in JavaScript. Therefore, in Swift, if you know you will not need to change the variable later in the code, stick to let, because it is a constant.

One of the key features in Swift is the ability to annotate variables. This means the user can assign a variable to be set as a specific value type, that cannot be changed later on. The type of annotations include Int, Double, String, and Bool. Integer for an Integer, Double for a floating point number, String for a Sequence of characters, and Bool for true, false values. This is something important to keep in mind when writing your first code in Swift.

Functions

To get started with functions, I am going to assume you are familiar with arrow functions (ES6). The syntax is similar with a few changes. First, instead of using “function” just use “func”. Then, follow the function variable name with parenthesis which is then followed by the arrow. The difference in the arrow is “->” instead of “=>”. After the arrow, you should include a return type. You should in most cases tell the function which data type is expected to return.

Conditionals (if, else if)

Conditionals behave the same in Swift as they do JavaScript, just with a bit less wording. In JavaScript, to use an if statement, you should generally wrap the conditional in parenthesis. Swift takes this away by not asking you to use parentheses around your conditional. Instead, just put the conditional after the “if” keyword, open brackets, and then start your code block as usual. The same applies to if else statements in the exact same way.

Comparison operators are almost the same, but with less characters. To check if something is strictly equal, you only need two equal signs, and to check the opposite, you only need a bang operator followed by one equal sign. The rest of the comparison operators are exactly the same.

Loops

Loops follow the same pattern as JavaScript. There will be a start condition, a repetition, and an end condition. Once again, the syntax is much shorter, and in my opinion, much easier to read and write when it comes to integers. To run a loop through two specific numbers, the starting condition is the first number, then three dots, then finish with the final number. This syntax is necessary when using the for-in loop within Swift.

If you want to add some more complexity to the loop, a good way is the use the stride() method. The three main inputs are “from”, “to”, and “by”. From is the starting condition, to is the end condition, and by is the method of iteration to use while going through the loop.

An important aspect to remember when writing loops is to use an underscore when necessary. If the variable following the for is not used in the loop, the loop will work, but will print a warning onto the console. Therefore, the simple fix is to replace the variable after the for with an underscore.

Switch statements behave similarly, except you must remember to use the “continue” keyword. Switch statements can take any number of arguments, each followed by a comma, and if none of the conditions are met, then the default will be returned if the continue keyword is used correctly. Continue must be used to exit the code block and keep moving down the code.

When you find the condition you are looking for, and want to break out of the loop immediately, you must use the “break” keyword. If not used, the loop will continue to run and look for more possibilities within the condition.

When you aren’t sure how many times you will need to loop, use a while loop. This behaves the same way as JavaScript, just without the parenthesis.

Arrays and Sets

In Swift, arrays can only contain the same value types, so we initialize the array to be our specific type. This can be done by initializing a variable to an array that contains one of the four data types, followed by a set of opening and closing parentheses. It’s almost as if you are invoking the array. An array may be created in the same as JavaScript, but once again, the data types must match, and the type of array is inferred.

Indexes are used in the exact same way. The first index is zero, the second is one, and so on. Count is also the exact same as using “length”. This will tell you how many elements are inside the array. Another example is “.apppend()”. This will behave in the same way as push.

The “insert” and “remove” methods are similar to using “splice” in JavaScript, but in my opinion, much more visually pleasing. To use these methods, just use the same starting syntax as splice, but within the parenthesis use the “at” keyword, followed by “:”, and then insert the index to be added or removed.

While these are the basics of Swift for someone with a previous knowledge of JavaScript, the best way to learn is do try it out yourself. So give it a go and good luck!

--

--