ES6 fundamentals Flashcards
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.
What is the purpose of the default keyword when exporting from a module?
The default keyword is used to export a single feature as the default export from a module.
What data structure does the JavaScript engine use to track the current function in execution?
The “function execution stack”
What is the purpose of the callback queue or task queue in JavaScript?
The callback queue or task queue in JavaScript is used to store and manage callback functions for asynchronous execution.
What are promises in JavaScript?
Promises are special objects in JavaScript that represent the eventual completion of an action.
How many states can a Promise be in, and what are they?
A Promise can be in one of three states: Pending (initial state), Fulfilled (successful completion), or Rejected (encountered an error).
What is the event loop’s priority when dealing with the job queue and callback queue?
The event loop gives priority to the job queue items over the callback queue items when the stack is free, ensuring that jobs are executed before callbacks.
What ia job queue in Javascript?
Every time a promise occurs in the code, the executor function of the promise gets into the job queue.
Who is responsible for managing the execution of asynchronous code and handling callback functions in a non-blocking manner in Javascript?
The event loop
What is the output of the main function?
function f1() {
console.log(‘f1’);
}
function f2() {
console.log(‘f2’);
}
function f3() {
console.log(‘f3’);
}
function main() {
console.log(‘main’);
setTimeout(f1, 50);
setTimeout(f3, 30);
new Promise((resolve, reject) =>
resolve(‘I am a Promise, right after f1 and f3! Really?’)
).then(resolve => console.log(resolve));
new Promise((resolve, reject) =>
resolve(‘I am a Promise after Promise!’)
).then(resolve => console.log(resolve));
f2();
}
main();
main
f2
I am a Promise, right after f1 and f3! Really?
I am a Promise after Promise!
f3
f1
How does the event loop execute tasks from the job queue and callback queue?
The event loop executes tasks from the job queue before moving to the callback queue when the stack is free, ensuring that jobs are given priority in execution.