Arrays & Objects Flashcards

1
Q

How do you tell if something is an array?

A

use Array.isArray(arr) which will return a boolean value

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

how do you find how many set values an array has?

A

While thelengthproperty of Array includes unset values in the count,Object.keysonly counts those values that have been set to some value:

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

What happens if you change an array’slengthproperty to a new, smaller value?

A

If you change an array’slengthproperty to a new, smaller value, the array gets truncated; JavaScript removes all elements beyond the new final element.

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

What is a const array?

A

variables declared withconstand initialized to an array are a little strange; while you can’t change what array the variable refers to, you can modify the array’s contents:

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

how do you stop an array from being modified?

A

If you want the elements of the array to also beconst, you can use theObject.freezemethod.
(This is a shallow freeze, objects in the array will still be mutable)

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

can you use === to compare arrays?

A

JavaScript treats two arrays as equal only when they are the same array: they must occupy the same spot in memory. This rule holds for JavaScript objects in general; objects must be the same object

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

What is weird about includes() and indexOf() when used with nested arrays?

A

includes and indexOfinternally use===to compare elements of the array with the argument. That means we can’t useindexOf or includesto check for the existence of a nested array or an object unless we have a reference to the same object or array we’re looking for:

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

What is array destructuring?

A

The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.

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

What is the syntax for array destructuring?

A

let a, b, rest;
[a, b] = [10, 20];

console.log(a);
// Expected output: 10

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

How can we access data from an object?

A

> person.name // dot notation
= ‘Jane’

> person[‘age’] // bracket notation
= 37

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

If you have a variable that contains an object keys name, how must you use it?

A

you must use bracket notation
> let key = ‘name’
> person[key]

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

How do you delete a property from an object?

A

> delete person.age
= true

> delete person[‘gender’]
= true

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

How can we define new methods?

A

Array.prototype.push = function(newValue) {
this[this.length] = newValue;
}

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

What is NOT an object or primative?

A
  • variables and other identifiers such as function names
  • statements such asif,return,try,while, andbreak
  • keywords such asnew,function,let,const, andclass
  • comments
  • anything else that is neither data nor a function
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do we create a new object that inherits from an existing object?

A

The static methodObject.createprovides a simple way to create a new object that inherits from an existing object:

let bob = { name: ‘Bob’, age: 22 };
let studentBob = Object.create(bob);
studentBob.year = ‘Senior’;

console.log(studentBob.name); // => ‘Bob’

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

How can we iterate through each element in an array?

A

using forEach()

17
Q

How can we iterate through each property in an object?

A

a for/in loop

18
Q

What is one downside of using a for/in loop?

A

One feature – or downside, depending on how you look at it – offor/inis that it iterates over the properties of an object’s prototypes as well. We can get around this using the hasOwnProperty method

19
Q

What does the JS date Object do?

A

JavaScriptDateobjects represent a single moment in time in a platform-independent format.Dateobjects encapsulate an integral number that represents milliseconds since the midnight at the beginning of January 1, 1970, UTC (theepoch).

20
Q

What is a for/of loop?

A

for/ofis similar tofor/in, but it iterates over the values of any “iterable” collection. For our purposes, the only iterable collections are arrays and strings.

let arr = [ 10, 20, 30 ]
for (let value of arr) {
console.log(value);
}

21
Q

What is optional chaining?

A

function reverse_words(sentence) {
return sentence?.split(‘ ‘)
.reverse()
.join(‘ ‘);
}
In this code, the?.operator performs optional chaining. Ifsentenceis nullish, the entire chain evaluates asundefined. Otherwise, the expression is evaluated as expected (assumingsentenceis a string):