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 trialDuc Duong
8,670 Points"readonly" and "const" type??
I wonder if these 2 types "readonly" and "const" have any differences?
1 Answer
Steven Parker
231,198 PointsA "const" can only be assigned by initialization during the compile. A "readonly" can be assigned in a constructor at run-time.
Kieran Barker
15,028 PointsKieran Barker
15,028 PointsThanks for clearing that up, Steven Parker :-)
Jun Dong
4,754 PointsJun Dong
4,754 PointsSo it's okay to switch out the const keyword and replace it with readonly and it'll work the same?
Steven Parker
231,198 PointsSteven Parker
231,198 PointsMy point was that they are different. For example, you couldn't use "const" for something that was going to get set to a value that is unknown until an object instance is being created as the program runs.