Objects Flashcards

1
Q

let user = new Object();

// the above is what type of object?

A

“object constructor” syntax

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

let user = {};

// the above is what type of object?

A

“object literal” syntax

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

an object has a property named?

A

key

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

an objects key contains a ____?

A

value

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

in a object a key can have multiple words but it must use ?

A

quoted

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

multi word keys will not work with what and must use _____?

A

dot notation
brackets

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

using bracket notation:
let user = {};

set a key “name” to “steve”

A

user[“name”]=”steve”;

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

let user = {
names = “steve”
};

delete names using bracket notation.

A

delete user[‘names’];

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

let user = {
names = “steve”
};

delete names using dot notation.

A

delete user.names

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

let user = {
name: “John”,
age: 30
};
// print “30” using dot notation
// print “30” using brackets

A

console.log(user.age)
console.log(user[“age”]);

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

write an object and add the number 5 to “bag” object

console.log(bag.apple);

A

let bag = {
[fruit]: 5,
};

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

function makeUser(name, age) {
return {
name: name,
age: age,
};
}

How can the above be shortened and why?

A

function makeUser(name, age) {
return {
name,
age,
};
}

properties have the same names as variables

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

let obj = {
for: 1,
let: 2,
return: 3
};

Will this give an error and why?

A

no

In short, there are no limitations on property names

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

let obj = {
0: “test”
};

alert( obj[“0”] ); // returns

A

test

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

let obj = {
0: “test”
};

alert( obj[0] ); // returns

A

test

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

let user = {};
console.log( user.noSuchProperty === undefined );

// returns and why?

A

true

here will be no error if the property doesn’t exist!

Reading a non-existing property just returns undefined

17
Q

let user = { name: “John”, age: 30 };

alert( “age” in user ); //
alert( “blabla” in user ); //

A

true

false

18
Q

let obj = {
test: undefined
};

alert( obj.test ); // Returns?
alert( “test” in obj ); // Returns?

and why

A

undefined

true

In the code above, the property obj.test technically exists. So the in operator works right.

19
Q

for unknown or empty values we use _____

A

null

20
Q

let user = {
name: “John”,
age: 30,
isAdmin: true
};

// the below returns?
for (let key in user) {
alert( key );
}

A

name, age, isAdmin

21
Q

let user = {
name: “John”,
age: 30,
isAdmin: true
};

// the below returns?
for (let key in user) {
alert( user[key] );
}

A

John, 30, true

22
Q

let codes = {
“49”: “Germany”,
“41”: “Switzerland”,
“44”: “Great Britain”,
“1”: “USA”
};

for (let code in codes) {
alert(code);
}

// returns and why?

A

1, 41, 44, 49

integer properties are sorted, others appear in creation order.

23
Q

let user = {
name: “John”,
surname: “Smith”,
age: 25
};

for (let prop in user) {
alert( prop );
}
returns?

A

// name, surname, age

24
Q

let user = {
name: “John”,
surname: “Smith”,
age: 25
};

//write a for in loop so it shows name, surname and age

A

for (let prop in user) {
alert( prop );
}

25
Q

let user = {
name: “John”,
surname: “Smith”,
age: 25
};

write a for in loop so it shows
john, smith, 25

A

for (let prop in user) {
alert( user[prop] );
}

26
Q

let codes = {
“49”: “Germany”,
“41”: “Switzerland”,
“44”: “Great Britain”,
“1”: “USA”
};

for (let code in codes) {
alert(code); /
}

what to do write make this print 1, 41, 44, 49.

A

add + to all keys