ES6 fundamentals Flashcards
(34 cards)
Define the concept of “scope”.
Scope is a way to identify where and whether we can use a variable.
What are the 3 types of scope?
- Block 2. Functional 3. Global
Are the variables declared with let
and const
available outside of their declaration block?
No, let
and const
variables have block scope - they are not visible outside of the block they were declared in.
Are the variables declared with var
, let,
const
accessible outside of the function they were declared in?
No, they are visible only inside the function.
Can you reassign variables declared with let
, const
and var
?
Only those decalred with let
and var
.
If you are in non-strict mode and you access an undefined variable declared withvar
, you get undefined
. What happens if you are in strict-mode?
You get ReferenceError
What is a template literal?
String literals that allow embedding expressions.
What is an arrow function?
An arrow function is an anonymous function with a special syntax: () => {}
How does this
keyword work in arrow function?
In arrow functions, this
is lexically bound, meaning it uses this
from the surrounding code that contains the arrow function.
When should you avoid using arrow functions and use regular functions instead?
You should avoid using arrow functions for object methods and event listener callbacks because this
behaves differently in arrow functions compared to regular functions.
In what situations is it recommended to use arrow functions?
Arrow functions are recommended when you need a concise coding style, such as for map
and reduce
operations.
What is object destructuring in ECMAScript 2015?
Object destructuring is the process of creating variables from object properties.
How can you use object destructuring to retrieve values from a nested object?
You can use object destructuring with nested objects like this: const { dept: { address: { street } } } = employee;
What is an alias in object destructuring?
An alias in object destructuring allows you to rename the variable when destructuring. For example, const { name: firstname } = user;
How can you handle dynamic property names with object destructuring?
You can handle dynamic property names with object destructuring using square brackets, like this: const { [key]: returnValue } = employee;
In what situations would you destructure objects in function arguments and return values?
Destructuring objects in function arguments and return values is useful when you want to work with specific properties of the object passed as an argument or returned from a function.
How do spread and rest syntax differ in ECMAScript 2015?
Spread takes an iterable (like an array) and expands it into individual elements, while rest syntax helps collect elements together in a destructuring context.
How can you clone an object using spread syntax?
You can clone an object using spread syntax like this: const newUser = { …user }
What is the difference between spread and rest syntax in assignment statements?
In assignment statements, rest syntax is used on the left side of the assignment to collect values, while spread syntax is used on the right side to spread values.
How can you merge two objects using spread syntax?
You can merge two objects using spread syntax like this: const final = { …user1, …user2 }
What is a JavaScript module?
A JavaScript module is a JavaScript file that encapsulates and exports features for use in other modules.
What keyword is used to export features from a module?
The export keyword.
How do you import features from another module?
Using import
keyword
How can you import all features of a module as a namespace?
You can import all features of a module as a namespace using the import * as NamespaceName from ‘module’; syntax.