DEV Community

Adrian Campos
Adrian Campos

Posted on

From JS to Swift: Key Differences Every Developer Should Know

Introduction

As a JavaScript developer, I was eager to dive into a new language and expand my programming skills beyond web development. That’s when I discovered Swift and the exciting world of Apple app development. With its clean syntax, modern design, and focus on performance, Swift provides powerful tools for building high-quality apps for iOS, macOS, and more. While JavaScript excels in web development, I wanted to shift gears and explore mobile app creation, diving into features like SwiftUI and type safety. Here’s a brief rundown of what I’ve learned about Swift—its uses, syntax, and the key differences between JavaScript and Swift.

Why learn Swift?

Image description

Performance: Swift is fast and safe, with tools like async/await and SwiftUI to make development easier. You can also fine-tune performance using Xcode’s profiling tools.

Native Apple Ecosystem Integration: Swift is the language of choice for developing native apps on iOS, macOS, watchOS, and TVOS. It seamlessly integrates with Apple’s frameworks, such as SwiftUI and UIKit.

Strong Community and Resources: Swift has a growing developer community and abundant learning resources, ensuring ongoing support and access to the latest tools for app development.

Data Types and Variables

Swift offers common data types like integers, strings, booleans, and floats. With type inference, you can often skip explicit type declarations. Use var for mutable variables and let for constants.

var isCompleted: Bool = false
var userAge: Int = 28
let username: String = "Alice"
Enter fullscreen mode Exit fullscreen mode

In this example:

isCompleted is declared as a Bool.

userAge is declared as an Int.

username is declared as a String.

Type annotation in Swift makes code safer and easier to understand. It also helps avoid mistakes, catch problems early, and make debugging and maintaining code more efficient.

# How Swift handles incorrect assignments
If the wrong data type is assigned to a variable, the compiler catches and throws and throws a mismatch error during compilation

var age: Int = 25 
age = "twenty-five" // Error: Cannot assign value of type 'String' to type 'Int'
Enter fullscreen mode Exit fullscreen mode

In this case, age is being reassigned to the string "twenty-five" resulting in an error because age was declared as an Int variable. This helps us catch these mistakes early on in development.

Functions
JavaScript and Swift share similar function syntax and string interpolation, but Swift is statically typed while JavaScript is dynamically typed. whereas JavaScript allows more flexibility, checking types only at runtime.

// Swift
func addNumbers(a: Int, b: Int) -> Int { 
     return a + b 
}

addNumbers(a: 3, b: 4) // Valid 
addNumbers(a: "3", b: 4) // Compile-time error
Enter fullscreen mode Exit fullscreen mode

This example demonstrates how mismatched parameter types like passing (“3”) as

a
Enter fullscreen mode Exit fullscreen mode

causes a compile-time error since

a
Enter fullscreen mode Exit fullscreen mode

must be an Integer instead of a string when calling addNumber.

//JavaScript
function addNumbers(a, b) {
    return a + b;
}

addNumbers(3, 4); // Valid, returns 7
addNumbers("3", 4); // Valid, returns "34" (string concatenation)
Enter fullscreen mode Exit fullscreen mode

Whereas JavaScript allows more flexibility, checking types only at runtime therefore concatenating the two inputs instead of getting the desired sum.

Arrays and Dictionaries
Arrays and dictionaries, like variables, can also be strictly typed, ensuring the data they hold remains consistent.

let numbers: [Int] = [30, 70, 2, 76, 52]
let data: [String: Int] = [
    "key_0": 90,
    "key_1": 51,
    "key_2": 91,
    "key_3": 67,
    "key_4": 57
]
Enter fullscreen mode Exit fullscreen mode

Key differences between Swift and JavaScript

Static and dynamic typing: I’ve already gone over how Swift is type annotated with its variable, arrays, dictionaries, and functions and Javascript is more flexible but another difference I wanted to mention is that in Swift, types are known at compile-time while Javascript types are determined during run-time leading to potential errors
Memory Management: Swift uses Automatic Reference Counting (ARC) to automatically track and manage memory usage for objects, while JavaScript uses Garbage Collection to clean up unused objects in memory.
Tooling: Swift development is centered around Xcode, Apple's IDE designed for building apps on iOS and macOS. On the other hand, JavaScript developers have more options. They use editors like VS Code or Sublime Text and rely on tools like npm to manage packages and Webpack to bundle their code.

Tips for transitioning from Javascript to Swift

Connect it back to your JavaScript knowledge: These two coding languages have many more similarities than differences as well as concepts like Control flow, Closures, and Object orientation. Put more emphasis on learning the syntax and additional features Swift has to offer
Revisit code problems and rewrite using Swift: I find it helpful when I solve a code problem in JavaScript and challenge myself to rewrite it using Swift syntax as well as just solving a new problem in Swift before attempting it in Javascript
Repetition: Practicing regularly will help your fluency with Swift’s syntax and will slowly become second nature as well as regularly working on projects in Xcode, hopefully learning something new every day
Documentation and video tutorials:
Learning the Essentials of Swift in one hour
Swift Introduction course
Swift documentation

Conclusion

Learning Swift as a Javascript developer has been a not-so-easy but incredible journey, coming from web development and transitioning to mobile apps had its set of challenges but was easier than starting from scratch with no developing experience. I strongly encourage anyone interested in mobile app development to give it a try! The idea that you can make a reality using Swift is limitless and has filled me with motivation to keep expanding my knowledge in development
Never stop learning!

Top comments (0)