Built-In Objects Flashcards

1
Q

What is the difference between encodeURI() and encodeURIComponent()?

A

They are both global functions.

encodeURI() assumes the input is a complete URI that might have some characters which need encoding.

const x = "http://www.yourdomain.com/a file with spaces.html”;
encodeURI(x);
// http://www.yourdomain.com/a%20file%20with%20spaces.html

encodeURIComponent() is used to encode query string parameters, in particular URLs.

const p = encodeURIComponent("http://xyz.com/?a=12&b=55");
const u = “http://domain.com/?param1=" + param1 + “&m2=99”;
// http://www.domain.com/?param1=http%3A%2F%2Fxyz.com%2F%3Fa%3D12%26b%3D55&m2=99

The difference between encodeURI and encodeURIComponent is that encodeURIComponent encodes the entire string whereas encodeURI ignores a URI’s domain related roots (protocol, prefix, and domain name)

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

How do you get an array of an object’s property names?

A

Object.keys returns an array of an object’s property names.

const foo = { x: 0, y: 0, z: 2};
Object.keys(foo); // [ 'x', 'y', 'z' ]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What can you do to an object to prevent properties from being added or removed, and prevent values of existing properties from being changed?

A

You can freeze the object with Object.freeze(). A frozen object can no longer be changed. This is a shallow freeze. It only applies to the immediate properties of the object.

const Days = Object.freeze({ Mon: 0, Tue: 1, ... });
Days.Mon = 77; // error
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you call a function and provide the ‘this’ value for the function?

A

Function.prototype.call() and Function.prototype.apply(). They also take arguments. apply() receives the arguments as a single array while call() accepts them as a list.

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

How do you add to the end of an array?

A

Array.prototype.push(): add to end of the array.

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

How do you remove from the end of an array?

A

Array.prototype.pop(): remove from end of the array.

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

How do you add to the beginning of an array?

A

Array.prototype.unshift(): add to beginning of array.

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

How do you remove from the beginning of an array?

A

Array.prototype.shift(): remove from beginning of array.

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

What does Array.prototype.unshift do?

A

Adds to beginning of array.

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

How do you find the index of a value in an array?

A

Array.prototype.indexOf(element, fromIndex): search from the start of the array (or fromIndex) and return the index where the value was found.

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

How do you make a shallow copy of a portion of an array?

A

Array.prototype.slice(begin, end): returns a shallow copy of a portion of an array into a new array object selected from begin to end (end not included). Does not alter original array.

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

How do you merge two or more arrays?

A

Array.prototyope.concat(): Merges two or more arrays. Does not change existing arrays. Returns a new array.

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

What does Array.prototype.filter(callback) do?

A

Creates a new array with all elements that pass the test implemented by the provided function.

const nums = [1, 2, 3, 4, 5];
nums.filter(x => x > 3); // [4, 5]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does Array.prototype.map(callback) do?

A

Creates a new array with the results of calling a provided function on every element in the calling array.

const nums = [1, 2, 3, 4, 5];
nums.map(x => x*2); // [2, 4, 6, 8, 10]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does Array.prototype.reduce() do?

A

Array.prototype.reduce(callback(accumulator, currentValue), initialValue): executes a provided reducer function on each element of the array, resulting in a single output value.

const nums = [1, 2, 3, 4, 5];
nums.reduce( (acc, cur) => acc + cur); // 15
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you test that at least one element in an array passes the test implemented by a provided function?

A

Array.prototype.some(callback)

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

How do you test whether all elements in an array pass the test implemented by a provided function?

A

Arrray.prototye.every(callback)

18
Q

How do you return the value of the first element in an array that satisfies the provided testing function?

A

Array.prototype.find(callback)

19
Q

How do you execute a provided function on each element of an array?

A

Array.prototype.forEach(callback)

20
Q

How do you return a pseudo-random number between 0 and 1?

A

Math.random()

21
Q

When does String.prototype.length not return the actual count of characters in the string?

A

String.prototype.length returns the length of the string in UTF-16 code units. UTF-16 uses a single 16-bit code unit to represent the most common characters, but needs to use two code units for less commonly-used characters. So if a string has one of these less commonly-used characters then String.prototype.length won’t return the actual count.

22
Q

What method do you use to determine whether one string is found in another string?

A

String.prototype.includes(search, position)

23
Q

How do you get the index within a string of the first occurrence of a given string?

A

String.prototype.indexOf(search, fromIndex)

24
Q

How do you extract a section of a string and return it as a new string?

A

String.prototype.slice(beginIndex, endIndex) extracts a section of a string and returns it as a new string without modifying the original. Up to but not including endIndex.

25
Q

How do you turn a string into an array of substrings, separating the string at each instance of a separator string?

A

String.prototype.split(separator)

26
Q

When would you use a Map object for storing key-value pairs instead of a regular object?

A
  • When keys are unknown until runtime.

* If there is a need to store primitive values as keys (objects can only have String or Symbol for keys)

27
Q

How do you add a key-value pair to a Map?

A

Map.prototype.set(key, value)

28
Q

What is a WeakMap?

A

A WeakMap is a collection of key-value pairs in which the keys are weakly referenced, which means that the WeakMap does not prevent garbage collection in case there would be no other reference to the key object.

29
Q

When is it useful to use a WeakMap?

A

A WeakMap is useful when mapping keys to information about that key that is valuable only if the key has not been garbage collected.

30
Q

What is the Set object?

A

The Set object is a collection of values, where the values are unique. A value in a Set can occur only once.

31
Q

How do you add a value to a Set?

A

Set.prototype.add(value)

32
Q

How do you convert an array to a Set?

A

const x = new Set([1, 2, 3, 4]);

33
Q

How do you convert a Set to an array?

A

const y = […mySet]

34
Q

What is the WeakSet object?

A

WeakSet is a collection of objects, where each object is unique and held weakly.

35
Q

What is a use for the WeakSet object?

A

WeakSet can be useful when tagging an object without mutating it. An example is cycle detection in JSON.stringify(), where it’s basically asking “has x already been seen?”

36
Q

What is JSON?

A

JSON is a syntax for serializing objects, arrays, numbers, strings, booleans, and null. JSON is short for JavaScript Object Notation.

37
Q

How do you convert a JSON string into a JavaScript object or value?

A

JSON.parse()

38
Q

How do you convert a JavaScript object or value into a JSON string?

A

JSON.stringify()

39
Q

What is NaN?

A

NaN is a property of the global object which represents “Not a Number”. This special value results from an operation that could not be performed because one of the operands was non-numeric (‘abc’/4) or because the result of the operation is non-numeric.

40
Q

What is the type of NaN?

A

typeof NaN; // ‘number’

41
Q

What are three ways to test for NaN?

A
  • Self-comparison: NaN, and only Nan, will compare unequal to itself.

NaN !== NaN; // true

  • isNaN(): returns true if the value is currently NaN or if it is going to be NaN after it is coerced to a number. Coercion rules are interesting so its suggested to use Number.isNaN() instead.

isNaN(‘blah’); // true

  • Number.isNaN(): returns true only if the current value is NaN.

Number.isNaN(‘blah’); // false