Basics Flashcards
What’s a unary operator?
A unary operator has one side.
It is used to easily add or subtract 1 from the value it is attached to. Written as ++ or –.
We often see it used in loops.
Eg:
myValue = 5
…. myValue++
What is the difference between let and const?
let allows you to change your variable whenever you want.
const does not.
When would you use const instead of let?
Use const for
What’s a ternary operator?
Shorthand syntax for if else statements.
condition ? expIfTrue : expIfFalse
num === 7 ? console.log(‘yay’) : console.log(‘naw’)
let colour = status === ‘online’ ? ‘green’ : ‘red’
The conditional statement can only be two sided, and therefore cannot include else if.
What’s switch?
Switch is shorthand syntax for when you have a series of else ifs. It uses the ‘switch’, ‘case’ and ‘break’ keywords.
Eg:
let day = 7
switch (day) { case 1: console.log('MONDAY'); break; case 2: console.log('TUESDAY'); break; case 3: console.log('WEDNESDAY'); break; case 4: console.log('THURSDAY'); break; case 5: console.log('FRIDAY'); break; case 6: console.log('SATURDAY'); break; case 7: console.log('SUNDAY'); break; default: console.log('INVALID DAY'); }
What’s the difference between let and const?
let can have its value reassigned whereas const cannot
HOWEVER
You can assign arrays and objects to const and then change their values . This is because the reference (the place in memory) stays the same for an array or object.
What’s the notation for an object?
{
key : value
}
What’s the notation for an array?
[1,2,3,4,5,6,7,8,9,10]
How do you access a nested array?
variable[idx][subIdx]
const animalPairs = [ [ 'doe', [ 'buck', 'stag' ] ], [ 'ewe', 'ram' ], [ 'peahen', 'peacock' ] ];
//To access 'ewe' animalPairs[1][0];
//To access 'buck' animalPairs[0][1][0];
//Updating a sub-array: animalPairs[0][1].push('hart');
How would you access data from an object?
1) Dot syntax:
const fitBitData = { totalSteps : 308727, totalMiles : 211.7, avgCalorieBurn : 5755, workoutsThisWeek : '5 of 7', avgGoodSleep : '2:13' };
console. log(fitBitData.totalSteps)
2) Square brackets with key in quotes
console. log(fitBitData[‘totalSteps’])
Use this if your key has spaces or some other unconventional form.
3) Square brackets containing a variable.
let choice = 'avgGoodSleep' console.log(fitBitData[choice])
This is useful if you’re taking dynamic input from elsewhere to then access a key on an object.
How do you update an object?
You can just reference the key and set it to the desired value.
The key does not need to be present on the object.
Eg: const obj = { } obj['first'] = 'no1'
obj = { first : ‘no1’ }
Is an object iterable?
No.
Arrays and strings are.
To access all keys/values in an object, wrap the method Object.keys(variable) inside a ‘for… of’ loop.
This is because the resulting value of Object.keys() will be an array, which you can then iterate over using a for… of loop.
Eg:
const movieReviews = { Arrival : 9.5, Alien : 9, Amelie : 8, 'In Bruges' : 9, Amadeus : 10, 'Kill Bill' : 8, 'Little Miss Sunshine' : 8.5, Coraline : 7.5 };
for (let movie of Object.keys(movieReviews)) {
console.log(You rated ${movie} - ${movieReviews[movie]}
);
}