JavaScript Flashcards
(118 cards)
What is a Promise?
A Promise is a special JavaScript object. It produces a value after an asynchronous (aka, async) operation completes successfully, or an error if it does not complete successfully due to time out, network error, and so on.
Successful call completions are indicated by the resolve function call, and errors are indicated by the reject function call.
What is asynchoronous?
Refers to the management of a single thread that can move past tasks that are waiting to complete the rest of the program. Synonymous with non-blocking.
Can you overwrite variable declarations without an error using var?
Yes; var camper = 'James'; var camper = 'David'; console.log(camper); // logs 'David'
What will let math = 12 + ‘13’; console.log to?
1213
What is the result of this: const s = [5, 6, 7]; s = [1, 2, 3]; // ???? s[2] = 45; // ?????? console.log(s); // ?????
s = [1, 2, 3]; // throws error, trying to assign a const
s[2] = 45; // works just as it would with an array declared with var or let
console.log(s); // returns [5, 6, 45]
Objects (including arrays and functions) assigned to a variable using const are still mutable. Using the const declaration only prevents reassignment of the variable identifier.
What function prevents data mutation of an object?
Object.freeze(obj);
const arr1 = ['JAN', 'FEB', 'MAR', 'APR', 'MAY']; let arr2;
arr2 = arr1;
arr1[0] = ‘OCT’
console.log(arr2); /// what does this log?
[ ‘OCT’, ‘FEB’, ‘MAR’, ‘APR’, ‘MAY’ ]
arr1 and arr2 are referencing the same array (object), so when arr1 is update, arr2 is updated.
To make an array that won’t change with arr1, use the spread operator:
arr2 = […arr1]
What does Array.shift() do?
Removes the first item from an array
let array = [‘today’, ‘was’, ‘not’, ‘so’, ‘great’];
let splice = array.splice(2, 2);
What are the values of array and splice?
array now equals [‘today’, ‘was’, ‘great’]
splice equals [‘not’, ‘so’]
(removes 2 elements beginning with the 2nd index)
Array.splice(starting index, how many to remove, (optional items to add in place))
const arr = ['cold', 'rainy', 'warm', 'sunny', 'cool', 'thunderstorms'] const newArr = arr.slice(2, 4) What is newArray?
[ ‘warm’, ‘sunny’ ]
arr.slice() returns a new array and takes in up to 2 arguments- first the starting index and second the ending index (not included).
arr will return the same array as it is not affected
What are data structures?
Techniques for storing and organizing data that make it easier to modify, navigate, and access. Data structures determine how data is collected, the functions we can use to access it, and the relationships between data.
What makes JavaScript functions first class objects?
a function can be assigned to a variable, passed around as an argument to another function and can also be returned from another.
What does this return?
console.log(square(5));
var square = function(n) { return n * n; }
TypeError: square is not a function
If you define a function as a variable, the variable name will be hoisted but you cannot access until JS execution encounters its definition.
If this was not assigned to a variable, would not return an error.
What are the six falsy values?
undefined, null, NaN, 0, “” (empty string), and false
Will these return true or false?
1) false == “”
2) NaN == undefined
3) 0 == null
4) null == undefined
5) NaN == NaN
6) 0 == “”
1) false == “”, true
2) NaN == undefined, false
3) 0 == null, false
4) null == undefined, true
5) NaN == NaN, false (NaN is not equivalent to anything, not even itself!)
6) 0 == “”, true
If can use Object.is(NaN, NaN) to see if it is equal (will return true)
What are the 6 primitive data types of JavaScript?
undefined Boolean Number String BigInt Symbol
How do you make a new object, using an existing object as the prototype of the newly created object?
let obj = Object.create(proto, [descriptors])
Example: let animal = { eats: true };
// create a new object with animal as a prototype let rabbit = Object.create(animal);
Technically, we can get/set [[Prototype]] at any time. But usually we only set it once at the object creation time and don’t modify it anymore. Changing a prototype “on-the-fly” with Object.setPrototypeOf or obj.__proto__= is a very slow operation as it breaks internal optimizations for object property access operations.
Can inherited properties of an object be overwritten?
Yes!
What is a JavaScript object?
An object is a collection of properties, and a property is an association between a name (or key) and a value. The object can contain any data types (numbers, arrays, object etc.)
Describe encapsulation.
Refers to bundling data with methods that can operate on that data within a class It is the idea of hiding data within a class, preventing anything outside that class from directly interacting with it Members of other classes can interact with the attributes of another object through its methods It’s generally best to not allow external classes to directly edit an object’s attributes Prevents data from getting into unwanted states
Describe abstraction
Refers to only showing essential details and keeping everything else hidden
Users of the classes should not worry about the inner details of those classes and should not directly interact with other classes’ data
If classes are entangled, then on change creates a ripple effect that causes many more changes
Creating an interface through which classes can interact ensures that each piece can be individually developed
What is the difference between encapsulation and abstraction
Abstraction is about hiding unwanted details while giving out the most essential details, while Encapsulation means hiding the code and data into a single unit e.g. class or method to protect the inner working of an object from the outside world. In other words, Abstraction means extracting common details or generalizing things.
What is polymorphism and what is the difference between static and dynamic polymorphism?
Polymorphism is the ability to call the same method on different objects and have each of them respond in their own way.
Dynamic:
-Occurs during the runtime of the program (when it is being executed)
-Describes when a method signature is in both a subclass and a superclass
-The methods share the same name but have different implementation
-The subclass that the object is an instance of overrides that of the superclass
Static:
-Occurs during compile-time rather than during runtime
-Refers to when multiple methods with the same name but different arguments are defined in the same class
-Despite the methods having the same name, their signatures are different due to their different arguments
What will this print: function Foo(){ console.log(this); }
It prints the window object because this is a global object. Whatever parent scope is, it will be inherited by the child.