Review Variables
VARIABLES
Nice work! This lesson introduced you to variables, a powerful concept you will use in all your future programming endeavors.
Let’s review what we learned:
- Variables hold reusable data in a program and associate it with a name.
- Variables are stored in memory.
- The
varkeyword is used in pre-ES6 versions of JS. letis the preferred way to declare a variable when it can be reassigned, andconstis the preferred way to declare a variable with a constant value.- Variables that have not been initialized store the primitive data type
undefined. - Mathematical assignment operators make it easy to calculate a new value and assign it to the same variable.
- The
+operator is used to concatenate strings including string values held in variables. - In ES6, template literals use backticks
`and${}to interpolate values into a string. - The
typeofkeyword returns the data type (as a string) of a value.
This part is just for review and understanding please revise it ok.
let catName = 'Minnie';
let catAge = 5;
let sentence = 'My cat is called ' + catName + ' , and is ' + catAge + ' years old.';
console.log(typeof sentence);
Instructions:
To learn more about variables take on these challenges!
- Create variables and manipulate the values.
- Check what happens when you try concatenating strings using variables of different data types.
- Interpolate multiple variables into a string.
- See what happens when you use
console.log()on variables declared by different keywords (const,let,var) before they’re defined. For example:
console.log(test1);
const test1 = 'figuring out quirks';
- Find the data type of a variable’s value using the
typeofkeyword on a variable. - Use
typeofto find the data type of the resulting value when you concatenate variables containing two different data types.
Solution:
1.console.log(test1);
var test1 = 'hahaha'
Terminal:
Nobins-MacBook-Pro:STOC_MODULE 11SEPT 2022 nobinpunyo$ node test.js
undefined
2.console.log(test1);
let test1 = 'hahaha'
Terminal:
ReferenceError: Cannot access 'test1' before initialization
3.console.log(test1);
const test1 = 'hahaha'
Terminal:
Comments
Post a Comment