✏️ 1.2.0.11 Practice: Variables and Strings
Goal: Practice creating and utilizing variables and strings by completing the exercises listed below. Variables and strings are both a fundamental building block of programming, so begin getting comfortable with them now!
In the Variables lesson, we learned:
- Variables, in JavaScript, are written in lower camelCase.
- In modern JavaScript, variables are created using
letandconst. - Variables can be used in arithmetic.
In the Strings lesson, we learned:
- Strings can include letters, punctuation, and numbers.
- Strings are surrounded with quotation marks (double or single).
- Variables can be set equal to strings.
- To escape characters use the backslash
\character.
Warm Up
- When are
letandconstused? What do they mean — and how are they different fromvar? - How can we use a variable after we've defined it?
- How do we declare a string?
- Can we create strings with either single or double quotes?
- What's the difference between
"9"with quotes and9without?
Code
Variables and Strings Practice
Here are a few exercises for you to practice using variables in the DevTools console:
- Set a variable called
someNameequal to your name in a string. We should be able to reassign the value ofsomeName, so choose whether you should useletorconstto declare this variable. - Display the value of
someNamein the console. - Change the value of
someNameto another name. - Set a variable called
favoriteNumberequal to your favorite number. The variablefavoriteNumbershould not be able to be reassigned, so choose whether you should useletorconstto declare this variable. - Calculate what your favorite number divided by 2 is, and save the result in a new variable called
halfOfFavNum. This variable should not be able to be reassigned, so choose whether you should useletorconstto declare this variable. - Set another variable called
michaelsFavoriteequal to13. We should be able to reassign the value ofmichaelsFavorite, so choose whether you should useletorconstto declare this variable. - Subtract your favorite number from Michael's.
- Change the value of
michaelsFavoriteto be 26 times its current value.