Jeopardy Flashcards
What are tenets of functional programming?
- Functional purity - no side-effects, output derived only from input
- Simple functions
- First class functions (functions as variables)
- Higher order functions (functions that return functions or accept functions as arguments)
What is a closure, and how/why would you use one?
- A closure is a function that maintains a reference to its outer scope.
- These are useful in high order functions.
What are the reference data types in JavaScript?
- Object. Just object.
What is the difference between ‘null’ and ‘undefined’
- Undefined has no value
- Null is explicitly set to an empty value
What are the primitive data types in JavaScript?
- Boolean
- Null
- Undefined
- Number
- String
- Symbol (new in ECMAScript 2015)
- Everything that is not a primitive data type is an object
What is the difference between a primitive data type and a reference data type?
- Primitive data types have a fixed amount of memory that they take up
- A reference data type is made up of references to other variables and does not take up a fixed amount of memory
- Primitive types include booleans or integers
- Reference types include arrays
Explain hoisting
- Hoisting is JavaScript’s method of moving declarations to the top of the current scope before the code in the rest of the scope executes
- Makes the function available everywhere in the scope
- To avoid bugs, always declare all variables at the beginning of every scope.
What creates scope in JavaScript?
- There’s global and function scope in ES5. In ES2015 there’s also block scope.
Name 2 or more ways to define a global variable in JavaScript
- Assign in the top level scope
- Leave the ‘var’ off and assign it to the ‘window’
How does inheritance work in JavaScript?
- Inheritance in JavaScript is prototypal.
- Objects are linked to other object instances via prototypes
Explain how prototypal inheritance works
- Every JavaScript object has a prototype property (empty by default), and you attach properties and methods on this prototype property when you want to implement inheritance.
- By adding methods and properties to a prototype, you make those methods and properties available later on.
What is the difference between ‘==’ and ‘===’ in JavaScript?
- ‘==’ (type coercison) checks for equality of value, while ‘===’ checks for equality of both value and data type.
What is async in JavaScript?
- Async is short for “asynchronous”
- Asynchronous code takes statements outside of the main program flow, allowing the code after the asynchronous call to be executed immediately without waiting.
- This allows multiple, independent tasks to be executed without slowing down the UI.
Explain the difference between synchronous and asynchronous functions.
- Synchronous functions must be executed in order. The first queued function must be completed before the second one is.
- Asynchronous functions are executed immediately, without waiting for other functions to execute or resolve.
What is event loop?
- The event loop listens for queued up messages and adds instructions to the call stack
What does the ‘this’ refer to in Javascript
- The first value passed to call() or apply()
- The value that was binded to the function
- The calling object
- The global scope
What is a data type?
- A classification of data which tells the compiler or interpreter how the programmer intends to use the data.
Why does (1 /3).toFixed(2) + 3
equal 0.333
?
- toFixed() coerces to a string, which makes + act as a concatenation operator, so it appends 3 to the resulting string
How big can a number be in JS?
- The range of safe numbers in JS is -(2^53 - 1) to (2^53 -1). This is roughly 9 quadrillion values
Why doesn’t .1 + .2 = .3 in JS?
- JS stores data as double-precision floating point numbers, which can’t accurately represent the infinite amount of decimal numbers that exist
What is scope?
- Scope is a container where references to variables and functions are shared
What is the difference b/w var, let and const?
- Var is scoped to the function, let and const are scoped to the block.
- Cosnt cannot be reassigned
Why is it dangerous to pollute or use the global scope?
- Libraries and frameworks might have modified the global scope, which makes it unreliable.
You declare a variable in the middle of a scope. Where is that variable accessible?
- Anywhere after the declaration in the same scope, as well as any nested scopes created after the declaration.
What does it mean to shadow a variable?
- Redirecting a variable that’s available in an outer scope, which removes access to it
What is the difference between declaring a function vs declaring a function expression?
- A declared function is hoisted, a function expression is not
What is the DOM?
- The document object model, the API the browser exposes for web pages
How do you create a div element?
- const div = document.createElement(‘div’)
What are 5 ways you can access a DOM element?
- document.querySelector
- document.querySelectorAll
- document.getElementById
- document.getElementsByClass
- document.getElementsByTagName
How do you access the ‘form’ element on a form submission event?
- event.target
How do you set the background-color of a given DOM element to red?
- element.style.backgroundColor = red
How do you access the ‘src’ attribute of an ‘img’ element?
- WIth ‘img.src’ or
img['src']
What’s the difference between setting the innerHTML
of an element and using appendChild()
?
- innerHTML completely replaces the content and takes a string; appendChild() adds to the element’s children and takes a DOM element
What is event bubbling?
- An event that happens on an element also happens on all parent elements sequentially unless
event.stopPropagation
is called
What are the 3 phases of a DOM event firing?
- Capture (going from the body down to the element that fired)
- Target (when the innermost element fires)
- Bubbling (when the event comes back through the DOM)
Compare and contrast a NodeList and an Array
- Both are collections, but they have different prototypes and therefore different methods available.
How would you check if a number is an integer?
- Check and see if there is a remainder left when dividing by 1
> function isInt(num) {
> return num % 1 === 0
}
console. log(isInt(4)) // true
console. log(isInt(12.2)) // false
What will the following code output?
> (function() { >> var a = b = 5: > }) ();
console.log(b)
- Since b is not declared anywhere in the function w/ var, it is set to equal 5 in the global scope.
The code is interpreted as such:
var a = b b = 5
Write a fn that will allow you to do this: multiply(5)(6)
- This is where you would create a closure, thereby keeping the value of the outer function even after the inner function is returned.
> function multiply(a) { >> return function(b) { >>> return a * b; >> } > }
multiply(5)(6) // 30
When would you use a bind function?
- When you have a particular fn that you want to call with a specific ‘this’ value. You can then use bind to pass a specific object to a fn that uses a ‘this’ reference
> function fullName() {
> return ‘hello, this is ‘ + this.first + ‘ ‘ + this.last;
// create a person object and pass its values to fullName fn
> var person = (first: ‘Foo’, last: ‘Bar’);
console.log(fullName.bind(person)()) // Hello this is Foo Bar
What does ‘use strict’ do?
- ‘use strict’ is a literal entered at the top of a JS program or fn, and helps you write safer JS code by throwing an err if a global var is created by mistake.
A program like this will throw an err w/ use strict:
> function doSomething(val) {
> ‘use strict’;
> x = val + 10;
}
This will throw an err because ‘x’ was not defined and is being set to some value in the global scope.
How would you add your own method to the Array object so the following code would work?
> var arr = [1, 2, 3, 4, 5] > var avg = arr.average()
console.log(avg)
- Since this calls for adding a method to the global Array object, you should modify the Array prototype
> Array.prototype.average = function() { >> //calculate sum >> var sum = this.reduce(function(prev, cur) {return prev + cur;}); >> // return sum divided by # of elements >> return sum / this.length > }
console.log(avg); // 3
What is a callback function?
- A fn that is passed to another fn as an argument and is executed after some operation has been completed.
> function modifyArray(arr, callback) { >> // do something to arr here >> arr.push(100); >> // then execute the callback fn that was passed >> callback(); > }
> > var arr = [1, 2, 3, 4, 5]
> modify Array(arr, function() {
> console.log(‘arr has been modified’, arr)
}
How would you create a private variable?
- Create it as a local variable within a function, so that even if the function is executed the variable cannot be accessed outside of the function.