Ad space (banner)
๐ŸฆSwift Lessons
Lesson 3 / 68

Working with Variables

In this lesson you will learn the basics of variables in Swift and the difference between the two declaration keywords, let and var. This is for anyone searching for "Swift variables let var how to use".

A variable is like a labelled box that stores a value under a name. let is for a value you will not change and var for one you will. Writing \(variableName) inside a string embeds the value.

The sample code declares two constants, let name = "Alice" and let age = 14, and embeds them with string interpolation. Reaching for let by default guards against accidental rewrites, which reflects Swift's safety-first design philosophy.

A common stumbling block for beginners is choosing between let and var. The rule is let when the value never changes and var when it might, but until that becomes a habit it is easy to over-use var and end up with less idiomatic, less safe code.

In real-world development, type inference lets you leave the types out and start writing as casually as in JavaScript - another part of Swift's appeal.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Swift directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)