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?
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?
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?
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” }.
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.
What is the simplified version of the below code? Under what conditions can it be simplified?
This is possible when there is no function body and only a return value. The return keyword can then be omitted.
Can the below code be simplified? Why?
When an arrow function has a single parameter for its arguments, the parentheses enclosing the parameter may be omitted.
What will be the output of the below code?
8
Write the below code in arrow function syntax.
What is a default parameter?
A default parameter is a parameter that kicks in if no parameter is provided (it is undefined).
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.
In ES6 what is the spread operator?
What would this code line return?
An error, as the spread operator only works in place e.g. in an argument to a function or in an array literal.
What version of ES is the following code part of?
What would be the outcome?
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?
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?
name would have a value of the string John Doe, and age would have the number 34.
How do you rename object variables while distructuring?