Basics Flashcards

1
Q

What’s a unary operator?

A

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++

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the difference between let and const?

A

let allows you to change your variable whenever you want.

const does not.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

When would you use const instead of let?

A

Use const for

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What’s a ternary operator?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What’s switch?

A

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');
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What’s the difference between let and const?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What’s the notation for an object?

A

{
key : value
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What’s the notation for an array?

A

[1,2,3,4,5,6,7,8,9,10]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you access a nested array?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How would you access data from an object?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you update an object?

A

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’ }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Is an object iterable?

A

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]});
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly