JavaScript Flashcards

1
Q

Explain the difference between var, let, and const

A

Var is globally or locally scoped.
Const and let are block scoped.
Var and let variables can be updated later.
Const variables cannot be updated later.
Var can be re-declared.
Let and Const cannot be re-declared.
Const and let cannot be hoisted, while var can be.

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

What are the different data types?

A

String, Number, BigInt, Boolean, Undefined, Null, Symbol, Object

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

Explain hoisting

A

Hoisting is when all variable and function declarations are moved to the top of the scope.

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

What’s the difference between “==” and “===”?

A

”==” only compares values whereas “===” compares values and types.

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

Explain implicit type coercion

A

Implicit type coercion is the automatic conversion of a value from one data type to another.
Example: When a number is added to a string, the number is always converted to a string.

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

Is JavaScript a statically or dynamically typed language?

A

JavaScript is a dynamically typed language meaning the type of the variable is checked during run-time instead of during compile-time.

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

What is NaN?

A

NaN property indicates a value that is not a legal number.

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

Explain passed by value and passed by reference

A

JavaScript passes primitive data types by value and non-primitive data types by reference.
Passing by value means that the operater allocates a new space in memory and returns the address.
Passing by reference means that the operator directly passes the location of the original variable without allocating new space in memory.

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

What are the primitive and non-primitive types?

A

Primitive:
String, Number, BigInt, Boolean, Undefined, Null, Symbol
Non-Primitive:
Object

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

What’s the difference between primitive and non-primitive types?

A

Primitive types can only store a single value.

Non-primitive types can store multiple and complex values.

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

What is an Immediately Invoked Function?

A

It is a function that runs as soon as it is defined.

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

Explain Higher Order Functions

A

Higher Order Functions are functions that either take other functions as arguments or return other functions.

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

Explain “this” keyword

A

The “this” keyword refers to the object that the function is a property of and the value will always depend on the object that is invoking the function.

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

Explain call(), apply(), and bind()

A

These are all predefined methods in JavaScript.
call() invokes a method by specifying the owner object and can take arguments (separated by commas).
apply() is similar to call, but takes arguments as an array.
bind() returns a new function where the value of “this” keyword will be bound to the owner object which is provided as a parameter.

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

What is currying?

A
Currying is a technique to transform a function of arguments n to n function of one of less arguments.
Example:
function add (a) {
  return function(b){
    return a + b;
  }
}

add(3)(4)

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

Explain Scope and Scope Chain

A

Scope determines the accessibility of variables and functions within your code.
Scope chain is JavaScript engine’s process of finding a variable within the code. It starts by trying to find the variable in local scope, then moves to outer scope, and finally moves to global scope. If the variable is still not found, it will throw a reference error.

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

What types of scope are there and what are the differences?

A

Global Scope - variables and functions can be accessed from anywhere inside the code.
Function Scope - variables and functions can only be accessed within the function they have been declared.
Block Scope - Related to let and const. Var does not have block scope. Variables declared between the curly braces “{ }” can only be accessed within that block.

18
Q

Explain closures

A

Closure is the ability for a function to store a variable for further reference even after it is executed.

19
Q

What are object prototypes?

A

A prototype allows us to use properties and methods on an object even if the properties and methods do not exist on the current object.

20
Q

What are callbacks?

A

Functions that are used as an argument to another function are called callback functions.

21
Q

What is memoization?

A

Memoization is when the return value of a function is cached based on its parameters. It’s used for heavy-duty / expensive functions. If the same paramter is used, the cached result will be return instead of computing a new result to save time.

22
Q

What is recursion?

A

Recursion is when a function calls itself repeatedly until it arrives at a result.

23
Q

What is the use of a constructor function?

A

Constructor functions are used to create multiple objects which have similar properties and methods.

24
Q

What is DOM?

A

DOM stands for Document Object Model. It represents the objects that comprise the structure and content of a document on the web.

25
Q

What are arrow functions?

A

Arrow functions are a new and shorter way to declare functions. They were introduced in ES6.

26
Q

Where should the “script” tag be inserted into an HTML file?

A

It can be inserted in the head or the body section depending on when you want your JavaScript to load. Some will suggest always putting it at the bottom of the body section so that errors in your JavaScript don’t prevent other sections of your webpage from loading.

27
Q

What is a Promise?

A

A Promise is a JavaScript object that represents the eventual success or failure of an asynchronous operation and the resulting value. It acts as a proxy for a value that is not necessarily known when the promise is created.

28
Q

What is AJAX?

A

AJAX is the use of the XMLHttpRequest object that allows you to communicate with a server, exchange data, and update the page without having to refresh the page.

29
Q

What is a RESTful API?

A

To be considered a RESTful API, the application programming interface must adhere to 6 constraints:

  1. Client-server (separation of user interface and data storage)
  2. Stateless (Session state is kept entirely on the client)
  3. Cacheable (Meaning the client cache has the right to reuse the response data for later, equivalent requests)
  4. Uniform interface (Architechtual constraints are used to guide the behavior of components)
  5. Layered system (Each component cannot “see” beyond the immediate layer with which it is interacting)
  6. Code on demand (This is optional and means client functionality to be extended by downloading and executing code in the form of applets or scripts)
30
Q

What is the benefit of using async/await?

A

It allows you to perform other work while waiting for the result of a long running task and makes asynchronous code behave like synchronous code.

31
Q

What is the difference between asynchronous and synchronous code?

A

Synchronous code runs one task at a time and waits for each task to finish before moving onto the next task. Asynchronous code on the other hand, can move to another task before the previous task is finished.

32
Q

What is the difference between cookes, localStorage, and sessionStorage?

A

localStorage is saved to the browser and does not clear when the browser is closed.
sessionStorage is also saved to the browser, but only until the browser tab or window is closed.
cookies are initially stored locally, but are then transmitted to the server that requested them in the first place.

33
Q

What is prototypical inheritance?

A

It is the ability for one object to access properties and methods from another object. An example of this is creating an “Animal” object prototype with properties such as “numberOfLegs”, “species”, and “countryNativeTo”. Then creating a new object called “Dog” that inherits those properties from that prototype.

34
Q

How do you copy an array?

A

Although there are multiple ways to do this, a few ways are by using either Array.from(), the spread operator […], or array.slice().

35
Q

How do you compare two objects?

A

One way is by doing the following:
JSON.stringify(obj1) === JSON.stringify(obj2);
Another is to convert them to a string by doing the following:
Object.entries(obj1).toString() === Object.entries(obj2).toString();
This will only work if the objects’ data is in the same order.
A better and deeper way of comparing your objects is by using a function that first checks the length of the properties to see if they are the same, then use a for loop to loop through the properties, comparing each one.

36
Q

Why is asynchronous programming important to JavaScript?

A

Since JavaScript is originally synchronous, the use of callbacks, promises, and async/await have made it possible to have multiple operations running at the same time, making the application faster by not blocking functions and code further down in the call stack from running before a previous function has finished its operation.

37
Q

What is the difference between null, undefined, and undeclared?

A

Undeclared means that the variable has not been declared with an appropriate keyword.
Undefined is when a variable has been declared, but not been assigned a value.
Null is assigned to a variable intentionally and represents the absence of a value.

38
Q

What is the difference between .map() and .filter() and .forEach()?

A

.map() iterates over each item in an array, transforms the item, and returns a new array with the same number of items as the original array.
With .filter() you are required to return a true or false condition which, when met, returns the element(s) you are looking for.
A .forEach() iterates over the array and does something with the items in the array without returning anything.

39
Q

How do you check for undefined and null?

A
undefined:
if(typeof(value) === "undefined") {
  return "undefined"
}
null:
if(typeof(value) === "object" && !value) {
  return "null"
}
40
Q

How do you copy an object?

A

With the spread operator (…)