ES6 Flashcards
What are the differences in scope of the var and let keywords?
When you declare a variable with the var keyword, it is declared globally, or locally if declared inside a function.
The let keyword behaves similarly, but with some extra features. When you declare a variable with the let keyword inside a block, statement, or expression, its scope is limited to that block, statement, or expression.
What will the below code return?
Why?
![](https://s3.amazonaws.com/brainscape-prod/system/cm/385/126/905/q_image_thumb.png?1653285135)
It will return “3” because a stored function will always return the updated (global) value of a variable declared using the “var” keyword.
What will this code return?
Why?
![](https://s3.amazonaws.com/brainscape-prod/system/cm/385/127/026/q_image_thumb.png?1653285686)
It will return “2” because three different i variables with unique values (0, 1, and 2) were created by the let keyword within the loop statement only; and throw an error that i is not defined because i was not declared in the global scope. It is only declared within the for loop statement.
What are the major differences between var, let and const?
These are all keywords for variable declaration.
var is the ES5 stardard one and it has global scope (unless it is declared inside a function). let does not allow a variable to be re-declared and has only local scope. const has the same attributes than let and, in addition, it does not allow the values originally assigned to a variable to be changed.
What will be the output of the below code?
What does that imply regarding the mutabilty of const?
![](https://s3.amazonaws.com/brainscape-prod/system/cm/385/276/983/q_image_thumb.png?1653392231)
s = [1, 2, 3] will result in an error. The console.log will display the value [5, 6, 45].
This means that const objects are still mutable enven tho re-agnement of const variables are illegal.
You can mutate the object [5, 6, 7] itself and the variable s will still point to the altered array [5, 6, 45]. Like all arrays, the array elements in s are mutable, but because const was used, you cannot use the variable identifier s to point to a different array using the assignment operator.
In ES6, how is an object nade immutable?
By using the function Object.freeze
In the code, below, the obj.review and obj.newProp assignments will result in errors, because our editor runs in strict mode by default, and the console will display the value { name: “FreeCodeCamp”, review: “Awesome” }.
![](https://s3.amazonaws.com/brainscape-prod/system/cm/385/876/957/a_image_thumb.png?1653845990)
What is an annonymous function? Why would you create one?
A function without a name. You would create one when you use such a funtion as an argument of another function i.e. a non-reusable function.
Re-write the below code in arrow function syntax.
![](https://s3.amazonaws.com/brainscape-prod/system/cm/385/887/231/q_image_thumb.png?1653850185)
![](https://s3.amazonaws.com/brainscape-prod/system/cm/385/887/231/a_image_thumb.png?1653850195)
What is the simplified version of the below code? Under what conditions can it be simplified?
![](https://s3.amazonaws.com/brainscape-prod/system/cm/385/888/616/q_image_thumb.png?1653850329)
This is possible when there is no function body and only a return value. The return keyword can then be omitted.
![](https://s3.amazonaws.com/brainscape-prod/system/cm/385/888/616/a_image_thumb.png?1653850361)
Can the below code be simplified? Why?
![](https://s3.amazonaws.com/brainscape-prod/system/cm/385/889/206/q_image_thumb.png?1653850786)
When an arrow function has a single parameter for its arguments, the parentheses enclosing the parameter may be omitted.
![](https://s3.amazonaws.com/brainscape-prod/system/cm/385/889/206/a_image_thumb.png?1653850950)
What will be the output of the below code?
![](https://s3.amazonaws.com/brainscape-prod/system/cm/385/890/048/q_image_thumb.png?1653851113)
8
Write the below code in arrow function syntax.
![](https://s3.amazonaws.com/brainscape-prod/system/cm/385/890/305/q_image_thumb.png?1653851272)
![](https://s3.amazonaws.com/brainscape-prod/system/cm/385/890/305/a_image_thumb.png?1653851421)
What is a default parameter?
A default parameter is a parameter that kicks in if no parameter is provided (it is undefined).
![](https://s3.amazonaws.com/brainscape-prod/system/cm/385/891/891/a_image_thumb.png?1653851718)
What is the rest parameter?
An ES6 syntax that allows us to pass as many arguments as we want into a funtion. These arguments are stored in an array that can be accessed later from inside the function.
![](https://s3.amazonaws.com/brainscape-prod/system/cm/385/896/011/a_image_thumb.png?1653854441)
In ES6 what is the spread operator?
![](https://s3.amazonaws.com/brainscape-prod/system/cm/387/738/678/a_image_thumb.png?1655278440)
What would this code line return?
![](https://s3.amazonaws.com/brainscape-prod/system/cm/387/739/161/q_image_thumb.png?1655278649)
An error, as the spread operator only works in place e.g. in an argument to a function or in an array literal.
![](https://s3.amazonaws.com/brainscape-prod/system/cm/387/739/161/a_image_thumb.png?1655278775)
What version of ES is the following code part of?
What would be the outcome?
![](https://s3.amazonaws.com/brainscape-prod/system/cm/387/739/375/q_image_thumb.png?1655279055)
ES5
name would have a value of the string John Doe, and age would have the number 34.
What version of ES is the following code belongs to?
What is this type of procedure named?
What would be the outcome?
![](https://s3.amazonaws.com/brainscape-prod/system/cm/387/739/570/q_image_thumb.png?1655279292)
ES6
This is distructuring.
name would have a value of the string John Doe, and age would have the number 34.
Visualise the ES6 destructuring version of the code below.
What would be the outcome of both?
![](https://s3.amazonaws.com/brainscape-prod/system/cm/387/739/672/q_image_thumb.png?1655279349)
name would have a value of the string John Doe, and age would have the number 34.
![](https://s3.amazonaws.com/brainscape-prod/system/cm/387/739/672/a_image_thumb.png?1655279362)
How do you rename object variables while distructuring?
![](https://s3.amazonaws.com/brainscape-prod/system/cm/387/739/738/a_image_thumb.png?1655280173)