Swift Tutorials for Front-end Developers: Variables, Constants, and Data Types
Master Swift in 2024 and Start Developing Your Own iOS/macOS App

Welcome to the Mastering Swift tutorial series, in this article we will cover variables, constants, and data types in Swift.
Next, we launch Xcode and select “File” > “New” > “Playground”. Create a new Playground and name it “Variables”.
- Optional parameters, Variadic parameters, In-out parameters, and Function types
- Closure expressions, Trailing closures, and Escape closuress
- Defining enums, Enum raw values, and Enum associated values
- Classes and Class inheritance, Optional properties, Type properties, and Property observers
- Protocol Conformance, Protocol Composition, Protocol Inheritance
- Structure Stored Properties, Computed Properties, Type Properties, Property Observers
Declare and use variables
Use the var keyword to define variables in Swift. Variable values can be changed during their lifetime.
Swift Code
var name = "John"
name = "Bytefer"TypeScript Code
let name = "John";
name = "Bytefer";Declare and use constants
Use the let keyword to define constants in Swift. Once a constant is assigned a value, its value cannot be changed.
Swift Code
let birthYear = 2015
// Error: Cannot assign to value: 'birthYear' is a 'let' constant
birthYear = 2016TypeScript Code
const birthYear = 2015;
// Error: Cannot assign to 'birthYear' because it is a constant.
birthYear = 2016; Data types
Now you know how to define variables and constants. While defining constants and variables, we can explicitly declare the type of the variable. Below we will cover the common data types in Swift.
1. String type
The String type is a collection of character sequences.
Swift Code
var greeting: String = "Hello, Swift!"TypeScript Code
let greeting: string = "Hello, TypeScript!";2. Int type
The Int type is a special type with the same length as the native word length of the current platform:
- On 32-bit platforms, Int and Int32 are the same length.
- On 64-bit platforms, Int and Int64 are the same length.
Swift Code
var age: Int = 25TypeScript Code
let age: number = 25;3. Float/Double type
Float is a number that contains a decimal part, such as 3.14159, 1.0, etc.
- Float type represents 32-bit floating point numbers, you can use this type if you don’t need a high precision.
- Double type represents 64-bit floating point numbers, please use this type when you need to store very large or high-precision floating point numbers.
Swift Code
let radius: Float = 2.5
let pi: Double = 3.1415926535
TypeScript Code
const radius: number = 2.5;
const pi: number = 3.1415926535;4. Bool type
Swift Code
var completed: Bool = trueTypeScript Code
let completed: boolean = true;Type inference
Swift and TypeScript are both type-safe languages. They check types at compile time to ensure that code does not attempt to perform unallowed operations. In addition, they both support type inference, which means that the compiler can automatically infer the type of a variable.
Swift Code
// The type of the message variable is inferred to be a String.
var message = "Swift is amazing!"
// The type of the pages variable is inferred to be a Int.
var pages = 50TypeScript Code
// The type of the message variable is inferred to be a string.
let message = "TypeScript is awesome!";
// The type of the pages variable is inferred to be a number.
let numberOfPages = 50;Type alias
In programming, type aliasing is a way to give a new name to an existing type. This helps to improve the readability and maintainability of the code. In both Swift and TypeScript, we can use type aliases to define our own named types.
In Swift, we use the typealias keyword to create type aliases. In TypeScript, type aliases are created using the type keyword.
Swift Code
typealias SampleRate = Double
let standardSampleRate: SampleRate = 44100.0
let highQualitySampleRate: SampleRate = 96000.0TypeScript Code
type SampleRate = number;
const standardSampleRate: SampleRate = 44100.0;
const highQualitySampleRate: SampleRate = 96000.0;String interpolation
String interpolation is a way of embedding variables or expressions in strings, making them more flexible and dynamic. In both Swift and TypeScript, we can use string interpolation to build strings with dynamic content.
In Swift, string interpolation is accomplished by including variables or expressions in a string and using the \() syntax. In TypeScript, we use backquotes (``) to create template strings and the ${} syntax to insert variables or expressions.
Swift Code
let x = 2023
let y = 1
let result = "The sum of \(x) and \(y) is \(x + y)."
print(result)TypeScript Code
const x: number = 2023;
const y: number = 1;
const result: string = `The sum of ${x} and ${y} is ${x + y}.`;
console.log(result);This article introduces the variables, constants, and data types in Swift. By comparing it with TypeScript syntax, we hope it will help you understand Swift better. If you want to learn Swift, you can follow me on Medium or Twitter to read more about Swift and TS!






