Skip to content

Constants

Think of a Constant as a Box with Superglue

A constant is like having a box that you close with superglue. Once you close the box, you can’t open it again and you can’t change what’s inside. The box is stuck with glue, and the contents inside will stay the same forever. Once it’s closed, it’s permanent!

How the Superglued Box Works

  • Imagine you have a box where you put a toy inside, and then you use superglue to close it. Once it’s glued shut, you can’t open the box again to change the toy inside. The toy stays the same, forever.
  • Similarly, in programming, a constant is like a box that’s glued shut. Once you put something inside, you can’t change it. The value is locked and stays the same, just like how the toy in the glued box never changes.

How to Declare a Constant in Swift

In Swift, we use let to define a constant. Here’s an example:

Think about the number of days in a week. There will always be 7 days in a week. That’s a rule that never changes. Even if you travel to different places, move to a new city, or change your routine, the number of days in a week will always be 7. This is a constant!

let daysInWeek = 7

In this example:

  • let means this is a constant—something that won't change.
  • daysInWeek is the name of the rule.
  • 7 is the number of days in a week, which is a constant!

Once you say the number of days in a week is 7, you can never change it to 8 or 6.

Examples of Constants

Social and Practical Constants
  • Your Date of Birth (constant day, month, year)

    let birthday = "12/12/2024" // December 12, 2024
    
  • Fixed interest rate (for example, exactly 5%)

    let fixedInterestRate = 0.05
    
Software Engineering Constants
  • Google Play Console One-Time Fee (for example, 25 in USD)
    let googlePlayConsoleOneTimeFee = 25
    
  • Apple Developer Program Fee per year (for example, 99 in USD)
    let appleDeveloperProgramFeePerYear = 99
    
Chemistry Constants
  • Avogadro's number (atoms/molecules per mole)

    let avogadroNumber = 6.02214076e23
    
  • Ideal Gas Constant (R) in J/mol·K

    let idealGasConstant = 8.3144598 // J/mol·K
    
Mathematical Constants
  • Pi (π)

    let pi = 3.141592653589793238462643383279502884197169399375105820974944
    
  • Euler's number (e)

    let e = 2.718281828459045235360287471352662497757247093699959574966967
    
  • Speed of light in a vacuum (m/s)

    let speedOfLight = 299792458.0 // meters per second
    
  • Gravitational constant (G) — in m³·kg⁻¹·s⁻²

    let gravitationalConstant = 6.67430e-11 // m³·kg⁻¹·s⁻²
    

Engineering Constants
  • Young's Modulus for Steel (Pa = N/m²)

    let youngsModulus = 2.10e11 // Pa
    

  • Thermal conductivity of Copper (W/m·K)

    let thermalConductivityCopper = 398.0 // W/m·K
    

Key Points to Remember

  • A constant holds a value that is permanent and cannot be changed.
  • You use let in Swift to create a constant.
  • A constant is like a box that you close with superglue—once it’s sealed, you can’t open it again or change what’s inside.