Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,842 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,842 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