JavaScript variables Flashcards
General rules for naming variables
- cannot start with numbers
- case sensitive
- cannot be the same as keywords
What is a variable
It is a container for a value.
- It holds reusable data in a program and associates it with a name
var
a key word that declares new variable var myName = 'Bethani'; console.log(myName); // Output: Bethani
In this case, key word variable is being used.
- var is declaring the variable myName
- Bethani is the value being assigned to myName
- After the variable is declared, the string value ‘Bethani’ is printed to the console.
Where are variables stored
In memory
Preferred variable keywords
let is the preferred way to declare a variable for reassigning and const when you want it to remain constant
When variables have not been initialized
they store the primitive data type undefined.
Mathematical assignment operators and variables
make it easy to calculate a new value and assign it to the same variable
+ operator used for what in variables
used to concatenate strings held in variables
Since ES6 what are backticks ` and ${} used for
` backticks
${} placeholder
- used to interpolate values into a string.
typeof keyword
returns the data type (as a string) of a value