Javascript Flashcards
What is Node.js
Server-side JavaScript framework allowing programmers to run JavaScript from their terminals
What is ECMAScript
Standardized specification for JavaScript, currently on ES6 (ES2015)
Explain the difference between an expression and statement
Expressions return a value and need a semicolon. Statements are any line of code and generally don’t require semicolons
Two looping keywords
continue - skips the current iteration
break - exits the loop
What’s the difference between for loops and forEach
You can’t exit early from a forEach loop. for
loops are less descriptive but have different forms
Explain a for..in
loop
for (key in object) - iterates through enumerable properties of an object (key and values for example). When used with an array/string, the keys are numerical indexes.
Note: not recommended when sequence matters as iteration is in arbitrary order
Explain a for..of
loop
Utilizes Symbol.iterator.next() from ES6 with a nice wrapper for (variable of iterable) - works for iterable things like Array/Strings but will not work on Objects.
Of note, we can use it with NodeLists (which normally don’t have access to Array iteration methods like foreach)
How do switch statements differ in JS
Same setup as Ruby but we need to include a ‘break’ otherwise it’ll keep passing through cases
Explain what a callback function is
A callback function is a function that is passed to another function as an argument and is executed after some operation has been completed
How does prototype inheritance work in Javascript?
Each object has an internal property called prototype, which links to another object. The prototype object has a prototype object of its own, and so on - this is referred to as the prototype chain.
If you follow an object’s prototype chain, you will eventually reach the core Object prototype whose prototype is null, signalling the end of the chain. When you request a property which the object does not contain, JavaScript will look down the prototype chain until it either finds the requested property, or until it reaches the end of the chain.
This behaviour is what allows us to create “classes”, and implement inheritance.
An example of where the Prototype pattern is useful is the initialization of business objects with values that match the default values in the database. The prototype object holds the default values that are copied over into a newly created business object.
Explain what Arrays are in JavaScript
An array is an object that holds values (of any type) not particularly in named properties/keys, but rather in numerically indexed positions
What is THIS
keyword in JavaScript
In JavaScript this
always refers to the ‘owner’ of the function we’re executing, or rather, to the object that a function is a method of
What is closure in Javascript
A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives you access to an outer function’s scope from an inner function. In JavaScript, closures are created every time a function is created, at function creation time.
When the outer function exits, a reference to the inner function is returned so the inner function can be called
The inner function will have access to the variables in the outer function scope, even after the outer function has returned.
The closure has access to variable in three scopes:
- Variable declared in his own scope
- Variable declared in parent function scope
- Variable declared in global namespace
Use cases:
- DATA PRIVACY: data in parent function only available in parent function unless we define “privileged methods” to allow access (this would be the child function returned from the parent function, which has access to that data)
- STATEFUL FUNCTIONS: return values can be influenced by the internal state (recall those functions from Foundations that only run sometimes)
- PARTIAL APPLICATION/CURRYING
What is transpiling
A process of transforming and compiling to convert newer code into older code equivalents
Typically you insert the transpiler into your build process, similar to your code linter or your minifier. There are quite a few great transpilers for you to choose from:
Babel: Transpiles ES6+ into ES5
Traceur: Transpiles ES6, ES7, and beyond into ES5
Differences between ES5 and ES6
- arrow functions and string interpolation
- the const keyword (can mutate, but cannot reassign)
- block-scoped variables: the new ES6 keyword let allows developers to scope variables at the block level. Let doesn’t hoist in the same way var does.
- the new ES6 keyword let allows developers to scope variables at the block level. Let doesn’t hoist in the same way var does.
- class definition and inheritance: The new ES6 keyword let allows developers to scope variables at the block level. Let doesn’t hoist in the same way var does.
- for-of operator:The for…of statement creates a loop iterating over iterable objects.
- spread operator: for objects merging
- promises: promises provide a mechanism to handle the results and errors from asynchronous operations. You can accomplish the same thing with callbacks, but promises provide improved readability via method chaining and succinct error handling.
- modules “importing” and “exporting”