"JavaScript Loops, Arrays and Objects" was retired on December 18, 2020. You are now viewing the recommended replacement.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Ruby Basics!
You have completed Ruby Basics!
Variables can hold numbers, text, a calendar date, or any other piece of data that can be stored in your computer's memory.
- Variables in a programming language are like variables in algebra, but they can hold other things besides numbers. They can hold text, or a calendar date, or any other piece of data that can be stored in your computer's memory.
- When you assign a value to a variable, you're giving that value a name that you can refer to it by.
- You assign a value to a variable with a single equals sign:
=
number = 4
greeting = "hello"
- We can then use that variable anywhere we might use the original piece of data.
puts number # 4
puts greeting # "hello"
puts number + 2 # 6
puts 12 - number # 8
- If we change the value the variable holds, the remainder of the program will use that new value instead of the old one.
number = 6
greeting = "hi"
puts number # 6
puts greeting # "hi"
puts number + 2 # 8
puts 12 - number # 6
- We can even replace the value a variable holds in the middle of a program.
- Valid variable names - same as method names.
- All lower case
- Numbers are legal but rarely used
- Snake case - separate words with underscores.
word = "hi"
multiple_words = "hi there"
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up