Variables Flashcards
What are variables?
They are like containers that store information in the computer’s memory. Can be primitive data types
What can you do with variables?
1) Create a variable with a descriptive name
2) Store or update information stored in a variable.
3) Reference or “get” information stored in a variable
Whats the short hand for variable in ES6?
var
Note: Variables cannot start with numbers
-are case sensitive
-Cannot be the same as keywords https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Keywords
What is the =?
the assignment operator used to assign a value to a variable.
What is let?
signals that the variable can be reassigned a different value. Ex) let meal = 'Enchiladas'; console.log(meal); // Output: Enchiladas meal = 'Burrito'; console.log(meal); // Output: Burrito
What is const?
Variable that is constant, cannot be changed
What are the math assignment operators?
Symbols set to not mathematical operations: -=, *=, and /=
Ex)
let x = 20; x -= 5; // Can be written as x = x - 5 console.log(x); // Output: 15
let y = 50; y *= 2; // Can be written as y = y * 2 console.log(y); // Output: 100
let z = 8; z /= 2; // Can be written as z = z / 2 console.log(z); // Output: 4
What are the Increment and Decrement Operator?
increment operator (++) and decrement operator (–). Communicates the value of variable to increase or decrease by 1
How would I write a variable that prints out: ‘I own a pet armadillo.’
let myPet = 'armadillo'; console.log ("I own a pet " + myPet + ".");
What is string interpolation?
You to build a string template (Template literal) with backticks (` `) and insert a variable using ${var} -We interpolate `I own a pet ${myPet}.` into output: 'I own a pet armadillo.' Ex) const myPet = 'armadillo'; console.log(`I own a pet ${myPet}.`); // Output: I own a pet armadillo.
How do you check for data type of a variable?
const unknown1 = 'foo'; console.log(typeof unknown1); // Output: string
const unknown2 = 10; console.log(typeof unknown2); // Output: number
const unknown3 = true; console.log(typeof unknown3); // Output: boolean
What do you get if variables that have not been initialized display:
‘undefined’