JavaScript ES6 Flashcards
What is a code block? What are some examples of a code block?
A code block refers to the code within the curly braces.
Examples of code blocks include functions, loops, and conditionals.
What does block scope mean?
Block scope defines the availability of a variable to only within it’s code block
What is the scope of a variable declared withconstorlet?
Block scope
What is the difference betweenletandconst?
let can be reassigned
const cannot be reassigned
Why is it possible to.push()a new value into aconstvariable that points to anArray?
Pushing is changing the value to the array in memory space, but not reassigning the reference value.
How should you decide on which type of declaration to use?
Prefer const, unless a variable needs to be mutated.
What is the syntax for writing a template literal?
Back ticks start and end `` with any variables starting with dollar sign wrapped in curly braces.
Hello ${name}!
What is “string interpolation”?
The ability to substitute part of the string for the values of variables or expressions
What is destructuring, conceptually?
Assignment of javascript object properties to variables
What is the syntax forObjectdestructuring?
Variable declaration Curly brace with variable(s) *If renaming propertyname : new name* Assignment Operator Object to destructure
e.g.
let { firstName, lastName } = person;
What is the syntax forArraydestructuring?
Variable declaration
Square Bracket with variable(s)
Assignment Operator
Array to destructure
How can you tell the difference between destructuring and creatingObject/Arrayliterals?
Destructuring will have the variables in brackets or curly braces on the left side of the assignment operator, and an existing array or object on the right hand side
What is the syntax for defining an arrow function?
- Variable keyword
- Variable name
- Parameters
- Arrow
- Bracket (Only required if statement)
When an arrow function’s body is left without curly braces, what changes in its functionality?
- The function can only have one line of code
- The code block must be an expression (code that resolves to value)
How is the value ofthisdetermined within an arrow function?
- The value of this is determined by it’s parent code block (outward with arrow functions)
When is it necessary to have parenthesis for parameters?
- 0 or 2+ mandatory
- 1 not mandatory
What are the three states a Promise can be in?
- Pending: initial state, neither fulfilled nor rejected
- Fulfilled: Successul operation
- Rejected: Failed operation
How do you handle the fulfillment of a Promise?
- then() method attaches fulfillment and rejection handlers to the promise, and returns a new promise
How do you handle the rejection of a Promise?
- catch() method appends a rejection handler
What isArray.prototype.filteruseful for?
Returning a filtered array
What isArray.prototype.mapuseful for?
Creating a new array with all of the previous arrays elements modified
What isArray.prototype.reduceuseful for?
Returning the sum of an array
What is “syntactic sugar”?
An alternative style on an existing syntax to make the language more clear for human use.
What is thetypeofan ES6 class?
Function
Describe ES6 class syntax.
class Example constructor(a, b, c) { this.a = a this.b = b this.c = c }, methodName() {}
What is “refactoring”?
Restructuring existing code without changing its external behavior.
What is Webpack?
Webpackis astatic module bundlerfor modern JavaScript applications
How do you add adevDependencyto a package?
npm install –save-dev [package name]
What is an NPM script?
- Commands that would typically be entered in command line to perform an action with your project
- NPM script is stored within your package.json file under “scripts”
How do you execute Webpack withnpm run?
In package.json add to scripts:
“build”: “web pack”
npm run build
How are ES Modules different from CommonJS modules?
ESM is statically analyzable, which allows for tree shaking (process that removes unused code from bundles)
Enhanced syntax (import export vs require)
ESM is official
What kind of modules can Webpack support?
ECMAScript Modules
CommonJS Modules
AMD modules
What doesfetch()return?
APromisethat resolves to aResponseobject.
What is the default request method used byfetch()?
GET
How do you specify the request method (GET,POST, etc.) when callingfetch?
Using request options, as a second argument with a specified method.
What must the return value of myFunction be if this expression is possible:
myFunction()();
myFunction calls a function and that inner function executes immediately after
Therefore, myFunction’s return must be a function
What does this code do?
const wrap = value => () => value;
Returns a value
In JavaScript, when is a function’s scope determined; when it is called or when it is defined?
Upon definition
What allows JavaScript functions to “remember” values from their surroundings?
A closure