Questions II Flashcards

1
Q

What is the purpose of the race method in promise

A

Promise.race() method will return the promise instance which is firstly resolved or rejected.

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

What is a strict mode in javascript

A

Strict Mode is a new feature in ECMAScript 5 that allows you to place a program, or a function, in a “strict” operating context. This way it prevents certain actions from being taken and throws more exceptions. The literal expression “use strict”; instructs the browser to use the javascript code in the Strict mode.

Strict mode is useful to write “secure” JavaScript by notifying “bad syntax” into real errors. For example, it eliminates accidentally creating a global variable by throwing an error and also throws an error for assignment to a non-writable property, a getter-only property, a non-existing property, a non-existing variable, or a non-existing object.

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

What is the purpose of double exclamation

A

The double exclamation or negation(!!) ensures the resulting type is a boolean. If it was falsey (e.g. 0, null, undefined, etc.), it will be false, otherwise, it will be true. For example, you can test IE version using this expression as below,

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

What is the difference between null and undefined

A

Type of null is object
Indicates the absence of a value for a variable
Converted to zero (0) while performing primitive operations

Type of undefined is undefined
Indicates absence of variable itself
Converted to NaN while performing primitive operations

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

What is eval

A

The eval() function evaluates JavaScript code represented as a string. The string can be a JavaScript expression, variable, statement, or sequence of statements.

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

What is an event flow

A

Event flow is the order in which event is received on the web page.

When you click an element that is nested in various other elements, before your click actually reaches its destination, or target element, it must trigger the click event for each of its parent elements first, starting at the top with the global window object.

There are two ways of event flow

Top to Bottom(Event Capturing)
Bottom to Top (Event Bubbling)

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

What is event bubbling

A

Event bubbling is a type of event propagation where the event first triggers on the innermost target element, and then successively triggers on the ancestors (parents) of the target element in the same nesting hierarchy till it reaches the outermost DOM element(i.e, global window object).

from children to parent

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

What is event capturing

A

Event capturing is a type of event propagation where the event is first captured by the outermost element, and then successively triggers on the descendants (children) of the target element in the same nesting hierarchy till it reaches the innermost target DOM element.

from parent to children

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

Is JavaScript a compiled or interpreted language

A

JavaScript is an interpreted language, not a compiled language. An interpreter in the browser reads over the JavaScript code, interprets each line, and runs it. Nowadays modern browsers use a technology known as Just-In-Time (JIT) compilation, which compiles JavaScript to executable bytecode just as it is about to run.

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

What is the use of preventDefault method

A

The preventDefault() method cancels the event if it is cancelable, meaning that the default action or behaviour that belongs to the event will not occur. For example, prevent form submission when clicking on submit button and prevent opening the page URL when clicking on hyperlink are some common use cases.

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

What is the use of stopPropagation method

A

The stopPropagation method is used to stop the event from bubbling up the event chain.

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

What is the purpose JSON stringify

A

When sending data to a web server, the data has to be in a string format. You can achieve this by converting JSON object into a string using stringify() method.

Konwertuje obiekt JavaScript na ciąg znaków w formacie JSON (string)

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

How do you parse JSON string

A

When receiving the data from a web server, the data is always in a string format. But you can convert this string value to a javascript object using parse() method.

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

What is a polyfill

A

A polyfill is a piece of JS code used to provide modern functionality on older browsers that do not natively support it. For example, Silverlight plugin polyfill can be used to mimic the functionality of an HTML Canvas element on Microsoft Internet Explorer 7.

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

What are break and continue statements

A

The break statement is used to “jump out” of a loop. i.e, It breaks the loop and continues executing the code after the loop.

The continue statement is used to “jump over” one iteration in the loop. i.e, It breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.

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

What is tree shaking

A

Tree shaking is a form of dead code elimination. It means that unused modules will not be included in the bundle during the build process and for that it relies on the static structure of ES2015 module syntax,( i.e. import and export). Initially this has been popularized by the ES2015 module bundler rollup.

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

What is a conditional operator in javascript

A

The conditional (ternary) operator is the only JavaScript operator that takes three operands which acts as a shortcut for if statements.

var isAuthenticated = false;
console.log(
isAuthenticated ? “Hello, welcome” : “Sorry, you are not authenticated”
);

18
Q

What is the difference between proto and prototype

A

The __proto__ object is the actual object that is used in the lookup chain to resolve methods, etc.

Whereas prototype is the object that is used to build __proto__ when you create an object with new.

19
Q

What is a freeze method

A

The freeze() method is used to freeze an object. Freezing an object does not allow adding new properties to an object, prevents removing, and prevents changing the enumerability, configurability, or writability of existing properties. i.e. It returns the passed object and does not create a frozen copy.

20
Q

What is a rest parameter

A

Rest parameter is an improved way to handle function parameters which allows us to represent an indefinite number of arguments as an array. The syntax would be as below,

function f(a, b, …theArgs) {
// …

The rest parameter should be the last argument, as its job is to collect all the remaining arguments into an array. For example, if you define a function like below it doesn’t make any sense and will throw an error.

21
Q

What is a spread operator

A

Spread operator allows iterables( arrays / objects / strings ) to be expanded into single arguments/elements. Let’s take an example to see this behavior,

22
Q

What is the main difference between Object.values and Object.entries method

A

The Object.values() method’s behavior is similar to Object.entries() method but it returns an array of values instead [key,value] pairs.

23
Q

What is a WeakMap

A

The WeakMap object is a collection of key/value pairs in which the keys are weakly referenced. In this case, keys must be objects and the values can be arbitrary values.

24
Q

What are primitive data types

A

A primitive data type is data that has a primitive value (which has no properties or methods). There are 7 types of primitive data types.

string
number
boolean
null
undefined
bigint
symbol

SSBBNNU

25
Q

What are the different ways to access object properties

A

There are 3 possible ways for accessing the property of an object.

Dot notation: It uses dot for accessing the properties
objectName.property;

Square brackets notation: It uses square brackets for property access
objectName[“property”];

Expression notation: It uses expression in the square brackets
objectName[expression];

26
Q

What is an error object

A

An error object is a built in error object that provides error information when an error occurs.

It has two properties: name and message. For example, the below function logs error details,

27
Q

What are the various statements in error handling

A

Below are the list of statements used in an error handling,

try: This statement is used to test a block of code for errors
catch: This statement is used to handle the error
throw: This statement is used to create custom errors.
finally: This statement is used to execute code after try and catch regardless of the result.

28
Q

What are the two types of loops in javascript

A

Entry Controlled loops: In this kind of loop type, the test condition is tested before entering the loop body. For example, For Loop and While Loop comes under this category.

Exit Controlled Loops: In this kind of loop type, the test condition is tested or evaluated at the end of the loop body. i.e, the loop body will execute at least once irrespective of test condition true or false. For example, do-while loop comes under this category.

29
Q

What is an event loop

A

The event loop is a process that continuously monitors both the call stack and the event queue and checks whether or not the call stack is empty.

If the call stack is empty and there are pending events in the event queue, the event loop dequeues the event from the event queue and pushes it to the call stack.

The call stack executes the event, and any additional events generated during the execution are added to the end of the event queue.

30
Q

What is call stack

A

Call Stack is a data structure for javascript interpreters to keep track of function calls

(creates execution context) in the program. It has two major actions,

Whenever you call a function for its execution, you are pushing it to the stack.

Whenever the execution is completed, the function is popped out of the stack.

31
Q

What is an event queue

A

The event queue follows the queue data structure. It stores async callbacks to be added to the call stack. It is also known as the Callback Queue or Macrotask Queue.

EVENT QUEUE STORES ASYNC CALLBACKS!

Whenever the call stack receives an async function, it is moved into the Web API. Based on the function, Web API executes it and awaits the result. Once it is finished, it moves the callback into the event queue (the callback of the promise is moved into the microtask queue).

The event loop constantly checks whether or not the call stack is empty. Once the call stack is empty and there is a callback in the event queue, the event loop moves the callback into the call stack. But if there is a callback in the microtask queue as well, it is moved first. The microtask queue has a higher priority than the event queue.

32
Q

What is a decorator

A

A decorator is an expression that evaluates to a function and that takes the target, name, and decorator descriptor as arguments. Also, it optionally returns a decorator descriptor to install on the target object. Let’s define admin decorator for user class at design time,

33
Q

What is an Unary operator

A

The unary(+) operator is used to convert a variable to a number. If the variable cannot be converted, it will still become a number but with the value NaN. Let’s see this behavior in an action.

var x = “100”;
var y = +x;
console.log(typeof x, typeof y); // string, number

34
Q

What is typescript

A

TypeScript is a typed superset of JavaScript created by Microsoft that adds optional types, classes, async/await, and many other features, and compiles to plain JavaScript. Angular is built entirely in TypeScript and is used as the primary language.

35
Q

What happens if you write constructor more than once in a class

A

The “constructor” in a class is a special method and it should be defined only once in a class. i.e, If you write a constructor method more than once in a class it will throw a SyntaxError error.

36
Q

How do you call the constructor of a parent class

A

You can use the super keyword to call the constructor of a parent class. Remember that super() must be called before using ‘this’ reference. Otherwise it will cause a reference error.

37
Q

What is Minification

A

Minification is the process of removing all unnecessary characters (empty spaces are removed) and variables will be renamed without changing it’s functionality. It is also a type of obfuscation .

38
Q

What is an enum

A

An enum is a type restricting variables to one value from a predefined set of constants.

JavaScript has no enums but typescript provides built-in enum support.

39
Q

What are the different methods to find HTML elements in DOM

A

If you want to access any element in an HTML page, you need to start with accessing the document object. Later you can use any of the below methods to find the HTML element,

document.getElementById(id): It finds an element by Id

document.getElementsByTagName(name): It finds an element by tag name

document.getElementsByClassName(name): It finds an element by class name

40
Q

What is a void operator

A

The void operator evaluates the given expression and then returns undefined(i.e, without returning value). The syntax would be as below,