JavaScript Flashcards

1
Q

Explain event delegation

A

Event delegation is a technique involving adding event listeners to a parent element instead of adding them to the descendant elements. The listener will fire whenever the event is triggered on the descendant elements due to event bubbling up the DOM. The benefits of this technique are:

Memory footprint goes down because only one single handler is needed on the parent element, rather than having to attach event handlers on each descendant.

There is no need to unbind the handler from elements that are removed and to bind the event for new elements.

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

Explain how this works in JavaScript

A

There’s no simple explanation for this; it is one of the most confusing concepts in JavaScript. A hand-wavey explanation is that the value of this depends on how the function is called. I have read many explanations on this online, and I found Arnav Aggrawal’s explanation to be the clearest. The following rules are applied:

If the new keyword is used when calling the function, this inside the function is a brand new object.

If apply, call, or bind are used to call/create a function, this inside the function is the object that is passed in as the argument.

If a function is called as a method, such as obj.method() — this is the object that the function is a property of.

If a function is invoked as a free function invocation, meaning it was invoked without any of the conditions present above, this is the global object. In a browser, it is the window object. If in strict mode (‘use strict’), this will be undefined instead of the global object.

If multiple of the above rules apply, the rule that is higher wins and will set the this value.

If the function is an ES2015 arrow function, it ignores all the rules above and receives the this value of its surrounding scope at the time it is created.

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

Can you give an example of one of the ways that working with this has changed in ES6?

A

ES6 allows you to use arrow functions which uses the enclosing lexical scope. This is usually convenient, but does prevent the caller from controlling context via .call or .apply—the consequences being that a library such as jQuery will not properly bind this in your event handler functions. Thus, it’s important to keep this in mind when refactoring large legacy applications.

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

What is the difference between == and ===?

A

The == operator compares two values, without taking into account their types. === compares the values, but also makes their types into account. So 3==’3’ evaluates to True, but 3===’3’ evaluates to False.

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

What is the difference between textContent and innerHTML?

A

textContent is a concatenation of the text content of a node and its descendant. It is the fastest of the two.

innerHTML returns the HTML. It parses the text into HTML, which makes it slower.

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

What is the DOM?

A

DOM stands for the Documentation Object Models, and it is a World Wide Web Consortium standard.It defines a standard for accessing documents, and it can be used to access and change the content of the HTML.

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

what is a JavaScript object?

A

In JavaScript, an object is a standalone entity, with properties and type. Compare it with a cup, for example. A cup is an object, with properties. A cup has a color, a design, weight, a material it is made of, etc. The same way, JavaScript objects can have properties, which define their characteristics.

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

What are the JavaScipt data types?

A

There are seven data types: Boolean, Null, Undefined, Number, String, Symbol, Object. The first six are primitives, meaning data types that aren’t objects and do not have any methods.

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

What are the pop-up boxes types available in JavaScript?

A

There are 3 pop up boxes: Alert, Confirm and Prompt.

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

What do the break and the continue statements do?

A

The break statement is used to exit a current loop. On the other hand, the continue statement continues with the next announcement in the loop.

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

What is negative infinity in JavaScript?

A

The result of a negative number divided by 0.

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

Explain the differences between var, const and let.

A

var declarations are globally scoped or function scoped while let and const are block scoped.

var variables can be updated and re-declared within its scope; let variables can be updated but not re-declared; const variables can neither be updated nor re-declared.

They are all hoisted to the top of their scope. But while var variables are initialized with undefined, let and const variables are not initialized.

While var and let can be declared without being initialized, const must be initialized during declaration.

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

Explain the difference between null, undefined or undeclared variables.

A

Undefined variables have been declared, but no value exists for them. Null is a value of variables, as well as a type of object. Undeclared variables are those declared without the var, const or let keyword.

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

What is ECMAScript?

A

The standard specification for scripting languages. JavaScript is based on ECMAScript.

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

What is a promise?

A

An object that may produce a single value sometime in the future with either a resolved value or a reason for not being resolved

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

What is AJAX?

A

It stands for Asynchronous JavaScript and XML. We can send data to the server and get data without refreshing the page.

17
Q

Is JavaScript a case-sensitive language?

A

Yes, JavaScript is a case sensitive language. The language keywords, variables, function names, and any other identifiers must always be typed with a consistent capitalization of letters.

18
Q

Can you assign an anonymous function to a variable and pass it as an argument to another function?

A

Yes! An anonymous function can be assigned to a variable. It can also be passed as an argument to another function.

19
Q

What is the purpose of ‘This’ operator in JavaScript?

A

The JavaScript this keyword refers to the object it belongs to. This has different values depending on where it is used. In a method, this refers to the owner object and in a function, this refers to the global object.

20
Q

What is Callback?

A

A callback is a plain JavaScript function passed to some method as an argument or option. It is a function that is to be executed after another function has finished executing, hence the name ‘call back‘. In JavaScript, functions are objects. Because of this, functions can take functions as arguments, and can be returned by other functions.

21
Q

What is the difference between Attributes and Property?

A

Attributes - provide more details on an element like id, type, value etc.

Property - is the value assigned to the property like type=”text”, value=’Name’ etc.

22
Q

What is the difference between window & document in JavaScript?

A

JavaScript window is a global object which holds variables, functions, history, location.

The document also comes under the window and can be considered as the property of the window.

23
Q

What is the difference between innerHTML & innerText?

A

innerHTML – It will process an HTML tag if found in a string

innerText – It will not process an HTML tag if found in a string

24
Q

What is the reason for wrapping the entire content of a JavaScript source file in a function book?

A

This is an increasingly common practice, employed by many popular JavaScript libraries. This technique creates a closure around the entire contents of the file which, perhaps most importantly, creates a private namespace and thereby helps avoid potential name clashes between different JavaScript modules and libraries.
Another feature of this technique is to allow for an easy alias for a global variable. This is often used in jQuery plugins.