ES6 fundamentals Flashcards

1
Q

Define the concept of “scope”.

A

Scope is a way to identify where and whether we can use a variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the 3 types of scope?

A
  1. Block 2. Functional 3. Global
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Are the variables declared with let and const available outside of their declaration block?

A

No, let and const variables have block scope - they are not visible outside of the block they were declared in.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Are the variables declared with var, let, const accessible outside of the function they were declared in?

A

No, they are visible only inside the function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Can you reassign variables declared with let, const and var?

A

Only those decalred with let and var.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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?

A

You get ReferenceError

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a template literal?

A

String literals that allow embedding expressions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is an arrow function?

A

An arrow function is an anonymous function with a special syntax: () => {}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How does this keyword work in arrow function?

A

In arrow functions, this is lexically bound, meaning it uses this from the surrounding code that contains the arrow function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

When should you avoid using arrow functions and use regular functions instead?

A

You should avoid using arrow functions for object methods and event listener callbacks because this behaves differently in arrow functions compared to regular functions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

In what situations is it recommended to use arrow functions?

A

Arrow functions are recommended when you need a concise coding style, such as for map and reduce operations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is object destructuring in ECMAScript 2015?

A

Object destructuring is the process of creating variables from object properties.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How can you use object destructuring to retrieve values from a nested object?

A

You can use object destructuring with nested objects like this: const { dept: { address: { street } } } = employee;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is an alias in object destructuring?

A

An alias in object destructuring allows you to rename the variable when destructuring. For example, const { name: firstname } = user;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How can you handle dynamic property names with object destructuring?

A

You can handle dynamic property names with object destructuring using square brackets, like this: const { [key]: returnValue } = employee;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

In what situations would you destructure objects in function arguments and return values?

A

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.

17
Q

How do spread and rest syntax differ in ECMAScript 2015?

A

Spread takes an iterable (like an array) and expands it into individual elements, while rest syntax helps collect elements together in a destructuring context.

18
Q

How can you clone an object using spread syntax?

A

You can clone an object using spread syntax like this: const newUser = { …user }

19
Q

What is the difference between spread and rest syntax in assignment statements?

A

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.

20
Q

How can you merge two objects using spread syntax?

A

You can merge two objects using spread syntax like this: const final = { …user1, …user2 }

21
Q

What is a JavaScript module?

A

A JavaScript module is a JavaScript file that encapsulates and exports features for use in other modules.

22
Q

What keyword is used to export features from a module?

A

The export keyword.

23
Q

How do you import features from another module?

A

Using import keyword

24
Q

How can you import all features of a module as a namespace?

A

You can import all features of a module as a namespace using the import * as NamespaceName from ‘module’; syntax.

25
Q

What is the purpose of the default keyword when exporting from a module?

A

The default keyword is used to export a single feature as the default export from a module.

26
Q

What data structure does the JavaScript engine use to track the current function in execution?

A

The “function execution stack”

27
Q

What is the purpose of the callback queue or task queue in JavaScript?

A

The callback queue or task queue in JavaScript is used to store and manage callback functions for asynchronous execution.

28
Q

What are promises in JavaScript?

A

Promises are special objects in JavaScript that represent the eventual completion of an action.

29
Q

How many states can a Promise be in, and what are they?

A

A Promise can be in one of three states: Pending (initial state), Fulfilled (successful completion), or Rejected (encountered an error).

30
Q

What is the event loop’s priority when dealing with the job queue and callback queue?

A

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.

31
Q

What ia job queue in Javascript?

A

Every time a promise occurs in the code, the executor function of the promise gets into the job queue.

32
Q

Who is responsible for managing the execution of asynchronous code and handling callback functions in a non-blocking manner in Javascript?

A

The event loop

33
Q

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();

A

main
f2
I am a Promise, right after f1 and f3! Really?
I am a Promise after Promise!
f3
f1

34
Q

How does the event loop execute tasks from the job queue and callback queue?

A

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.