4 - Data Structures: Objects and Arrays Flashcards
How to access a property of an object?
Using the bracket, or a dot notation.
What do you retrieve by accessing a property?
Value that property references.
Which objects don’t have properties?
‘null’ and ‘undefined’
What is the return value of typeof null
?
‘object’
What is a requirement for a property name so that you can access it with a dot notation?
It must follow the rules of the bindings.
What property names do you use to retrieve values from an array?
Numbers. They start with ‘0’.
What is a method?
Property on an object that references a function value.
Conceptually, what kind of a data structure is an array?
not in the JS context
Stack. LIFO
What is a JS object in regard to what it contains?
Arbitrary collections of properties.
How to syntactically write a property name of an object which doesn’t adhere to the rules of naming a binding?
Using quotes.
What purposes have braces in general?
To use for a set of statements and a notation for an object literal.
What will be returned when you access a property that doesn’t exist on the object?
‘undefined’
How to remove a property from an object?
delete obj.foo
How to check if an object has a property?
‘a’ in object
How to find all properties of an object?
Object.keys(obj)
How to copy all properties from one object to another?
Object.assign(toObj, fromObj)
What is the return value of ‘typeof []’
object
Which data types are not mutable?
Strings, numbers, booleans, null and undefined
How to compare if two bindings have reference to the same object?
let o1 = { foo: 1 }
let o2 = o1;
o1 == o2 // true
Is this valid const o = { a: 1 }; o.b = 2? Why?
Yes.
It mutates the elements of an object and not the assignment.
How is the comparison with == of two different objects called?
Identity comparison.
How to write classical for loop in a easier way?
for (let elem of collection) {
for…of
What other objects can you loop over with for..of loop?
Strings, objects, but iterables in general.
What do ‘unshift’ and ‘push’ do?
unshift -> add element from the left
push -> add element from the right
What do ‘shift’ and ‘pop’ do?
shift -> remove element on the left
pop -> remove element on the right
What does indexOf do?
Finds the index of the first matching element. [1,2,3].indexOf(2) => 3
What do indexOf and lastIndexOf return when they can’t find an element?
-1
What does lastIndexOf do?
Finds the index of the first matching element searching from the right. [1,2,3].indexOf(2) => 1
How Array.slice behaves?
It creates a new array from the one it was called on.
[1, 2, 3, 4, 5].slice(2) => [3, 4, 5]
[1, 2, 3, 4, 5].slice(2, 4) => [3, 4]
How many elements Array.slice takes? Which element is inclusive, and which is exclusive?
Two elements. First one is inclusive, the second one is exclusive.
Will this work ‘[1, 2, 3, 4, 5].concat(3)’?
Yes.
It can concatenate non-array values.
How to join two arrays using a Array function?
Array.concat.
Why can’t you add a new property to a string?
Because strings are not objects.
Why do strings have properties even though they are immutable?
Because they inherit properties from String.prototype.
Do strings have .slice and .indexOf properties?
Yes.
What does String.trim do?
Removes whitespace from the start and end of the string.
How does String.padStart work?
‘6’.padStart(4, ‘1’) // => ‘1116’
How does String.split work?
It splits the string on a given char.
How does String.join work?
It doesn’t exist.
How to repeat a string ‘foo’ three times?
With String.repeat => ‘foo’.repeat(3)
How can you access letter ‘x’ in a string ‘000x00’?
‘000x00’[3]
How can you “collect” multiple arguments to a function to a single parameter?
With rest param. function foo(…args)
If you have an array and want to pass its elements to a function as arguments, how do you do it?
foo(…ary)
Why is spread operator called so?
Because it “spreads” elements of an array in place as separate values.
What is the Math object by itself technically? Why is it needed?
It acts as a namespace for Math.sum, Math.sqrt etc so that the global namespace isn’t polluted by those functions.
What does it mean to “pollute” a global namespace?
Global namespace is polluted when bindings are defined in the outer most scope. It increases the likelihood of overriding some binding.
How does Javascript warn you when you redeclare bindings created with var and function keyword?
It doesn’t.
What keywords do you have to use so that Javascript warns you when you redeclare a binding?
let and const
What is the return value of Math.random?
A pseudorandom number between (inclusive) zero and (exclusive) one.
How are random numbers conceptually created considering that computers are deterministic machines?
By creating a hidden value which gets modified every time a new random number gets created.
What is the return value of Math.floor(8.23)?
8
What is the opposite of Math.floor?
Math.ceil
Which function will round the given fractional number up or down?
Math.round
What is the return value of Math.round(0.50)?
1
How do you ensure you always get a positive number from any given number input?
Math.abs
If the function accepts an array with two elements as an argument, how do you assign those elements in the function signature?
function foo([firstElemt, secondElem])
What is the value of binding z here ‘let [z] = null’
Type error: null is not iterable
How many memory addresses are created with ‘let o = { a: [1, 2] }’
Two. One for the object “o” and the other for the object referenced by the “a” property.
How do you send Javascript literal object over the network?
You can’t. You have to serialize the object.
How do you serialize the Javascript object?
JSON.stringify
How do you deserialize JSON object?
There are no JSON objects. JSON is a string which is deserialized with JSON.parse.
What is the syntax for putting comments within JSON files?
JSON doesn’t support comments.
Which quotes are available in JSON format?
Double quotes.