Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Swift Basics (retired) Variables and Constants Variables

Jonathan Whelchel
Jonathan Whelchel
2,096 Points

var versus =

what is the difference between

var str = "playground"

and

str = playground

3 Answers

Michael Hulet
Michael Hulet
47,913 Points

The first one is creating a new variable str and assigning the String value of "playground" to it, but the second one is assigning the value of the already-created variable playground to the also already-created variable str

Jonathan Whelchel
Jonathan Whelchel
2,096 Points

What I'm saying is why do var at all when I can just do str =

Michael Hulet
Michael Hulet
47,913 Points

Because you can't do just str = whatever. Using the var keyword sets up the variable to be used for the first time. After that, you shouldn't use the var keyword, because that's only used for creating new variables