Built-In Objects Flashcards
What is the difference between encodeURI() and encodeURIComponent()?
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 do you get an array of an object’s property names?
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' ]
What can you do to an object to prevent properties from being added or removed, and prevent values of existing properties from being changed?
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 do you call a function and provide the ‘this’ value for the function?
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 do you add to the end of an array?
Array.prototype.push(): add to end of the array.
How do you remove from the end of an array?
Array.prototype.pop(): remove from end of the array.
How do you add to the beginning of an array?
Array.prototype.unshift(): add to beginning of array.
How do you remove from the beginning of an array?
Array.prototype.shift(): remove from beginning of array.
What does Array.prototype.unshift do?
Adds to beginning of array.
How do you find the index of a value in an array?
Array.prototype.indexOf(element, fromIndex): search from the start of the array (or fromIndex) and return the index where the value was found.
How do you make a shallow copy of a portion of an array?
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 do you merge two or more arrays?
Array.prototyope.concat(): Merges two or more arrays. Does not change existing arrays. Returns a new array.
What does Array.prototype.filter(callback) do?
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]
What does Array.prototype.map(callback) do?
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]
What does Array.prototype.reduce() do?
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 do you test that at least one element in an array passes the test implemented by a provided function?
Array.prototype.some(callback)