Swiftly JavaScript – From JavaScript to Apple’s Swift

Swiftly JavaScript – From JavaScript to Apple’s Swift
COMMENTS (0)
Tweet

This is a series of blogs that will present a comparative analysis of JavaScript and Apple’s Swift programming language. In this first post, I’m going to look at the basic syntactical comparison between JavaScript and Swift and in future posts I’ll explore both these languages in more detail. So in the first blog from the series, let’s look at the basic similarities between these two languages.

Although most programming languages have changed over time, Apple’s Objective-C language has remained more or less constant, that is until recently, when Apple announced a brand new programming language for its devices. This was a major change which caused a lot of excitement in the programming world, since everyone wanted to know what this new programming language would be like. The suspense was over soon though when Apple unveiled its new language called Swift that is different in form, but similar in essence to Objective-C.

After getting an initial hands-on with Swiftly, as someone who has worked on JavaScript, Python & C#, my first impressions were that it’s a lot like JavaScript and, in few areas, like Python & C#. The reason behind this is like most other programming platforms that use JavaScript as their base – like Unity for example – Swift is also based on JavaScript.

FACT: JavaScript is the top and trending language on GitHub, with almost 30% of all repositories present there in 2015, and it’s still on the rise.

In terms of syntax though there are some minor differences between JavaScript and Swift but not much. Let’s look at the below Hello World program as an example.

Here’s how we write Hello World in JavaScript


console.log("Hello World! :-)");

Here’s how you do it in Apple’s Swift:


print("Hello World! :-)");

As you can see from the code snippets above, they are more or less the same. And yes the semicolons ; are not-required in both languages 😉

 

Comparing syntactically

If you compare them syntactically, there are some subtle differences. For instance, in terms Variables. Both JavaScript and Swift use the var expression to define a new variable with a value (as shown below).


var blogName = "JS to Swift";

Additionally in Swift, constants can be defined as:


let MY_COMPANY = "Folio3 Inc.";

And therein lies the first difference. Since JavaScript is completely a dynamically typed language, so all the variables used in it are either strings, functions or objects, while in Swift, the language is strictly static type based, so you have to mention the types explicitly. For example in Swift, if you want to separate the definition and declaration, it would have to be like this:


var blogName: String;
blogName = "JS to Swift";

The same applies to constants, except the fact that you can declare them only once.


/* Mr. Nothing */

Mr. Nothing or null is an essential ingredient in dynamic typed languages including JavaScript), but as I mentioned earlier, Swift is a static typed language, so you cannot have Mr. Nothing everywhere. Here’s how it’s done in JavaScript.


var mrNothing = null;

In Swift on the other hand, you have nil and it can only be assigned to null-able or optional variables (as shown below).


var mrNothing: Int? = 0;
mrNothing = nil;

 

/* Strings */

In JavaScript, strings are defined using the opening and closing “double quotes”, as depicted in the example below. Notice the string concatenation using the + operator.


var hello = "Hello", world = "World";
console.log(hello + world);

Swift also uses the same methodology for defining strings but also provides built-in support for string templates, or string formatting in other words (as shown below).


var firstName = "Ammar", middleName = "Hasan", lastName = "Rizvi";
print("My full name is \(firstName) \(middleName) \(lastName)");

You can also perform different computations in these templates.

 

/* Dictionaries and Arrays */

In JavaScript, every object is actually a dictionary or vice versa, while in Swift you can define dictionaries. For example, we define dictionaries and arrays in JavaScript as follows:


var synonyms, items;
synonyms = {
"Swift": "JavaScript",
"JavaScript": "Swift"
};
items = ["Swift", "JavaScript"];

While in Swift, you define dictionaries and arrays as follows:


var synonyms: Dictionary<String, String>, items: Array;
synonyms = [
"Swift": "JavaScript",
"JavaScript": "Swift"
];

items = [“Swift”, “JavaScript”];

 

/* Commenting */

When it comes to adding comments to code, the methodology for doing that is the same in both JavaScript and Swift (as depicted below).


// JS to Swift
/* JS to Swift */

 

/* Loops and Iterators */

Loops in Swift are inspired by multiple languages, which happily include JavaScript also. Here’s one way of iterating an array in JavaScript


// Looping an array
for (var item in items) {
console.log("Item: " + item);
}

While in Swift, it is done like this:


// Looping an array
for item in items {
print("Item: \(item)");
}

To iterate an object or dictionary in JavaScript, you use the following code:


// Looping a dictionary
for (var word in synonyms) {
console.log("The synonym of " + word + " is " + synonyms[word]);
}

To do the same in Swift, you use the code below.


// Looping a dictionary
for (word, meaning) in synonyms {
print("The synonym of \(word) is \(meaning)");
}

There are also some very advanced loop statements in Swift, which reduce lots of coding effort in some places. But we’re not going to touch on those in this blog post.

 

/* Functions */

As you know, functions are the backbone of JavaScript. Everything revolves around functions in JavaScript, even classes are a variation of functions. There are subtle differences between both but structurally both languages follow the same syntax for functions.

Here’s how we write a function in JavaScript


function sayHello(personName) {
var greeting = "Hello, " + personName + "!";
return greeting;
}
var message = sayHello("Ammar");
console.log(message);

In Swift this is done as follows:


func sayHello(personName: String) -> String {
let greeting = "Hello, " + personName + "!";
return greeting;
}
var message = sayHello("Ammar");
print(message);

As you can see, the syntax used is very similar to JavaScript, the main difference being the declaration of arguments and return types in Swift, since it is a static typed language. There are few other special variations of functions in Swift also, which we’ll look at in a separate blog.


In Short

There are numerous similarities between both JavaScript and Swift. There are some minor differences but their essence is the same. As compared to JavaScript, Swift provides a lot of advancements in syntax. Of course (arguably) it can be said that the more features a language provides, the more trouble it can create. Since then you need to know most of the syntax in order to program effectively. In contrast, JavaScript is relatively simple with a very limited syntax, but it has its shortcoming as well.

However, given the similarities in syntax between both these languages, if you are a JavaScript programmer, you can learn Swift really fast and pretty easily. Bottom line – if you know JavaScript, you can learn Swift swiftly.

Explore our blog


ABOUT FOLIO3

As a leading mobile app development company (iPhone, Android, Windows Phone, HTML5 app development), Folio3 specializes in native app development services and cross platform mobile app development services for the iPhone and iPad. We also offer extensive mobile app testing and QA services. If you have a mobile app idea that you’d like to discuss please or would like to know more about our iPhone app development services, please Contact Us. Learn more about our iPhone, Android and Windows Phone app development services.

CALL

USA408 365 4638

VISIT

1301 Shoreway Road, Suite 160,

Belmont, CA 94002

Contact us

Whether you are a large enterprise looking to augment your teams with experts resources or an SME looking to scale your business or a startup looking to build something.
We are your digital growth partner.

Tel: +1 408 365 4638
Support: +1 (408) 512 1812