JavaScript Flashcards

1
Q

What are the DATA TYPES of JavaScript? (there are 8)

A

PRIMITIVE: string, number, boolean, BigInt, symbol, undefined, null,
NON-PRIMITIVE: object

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

What is HOISTING

A

Hoisting is a default behavior of JavaScript where all the variable and function declarations are moved on top. Declarations can be hoisted in the global or local scope. Only declarations are hoisted, not initializations. Also, function expressions (functions assigned to a variable, including arrow functions) are not hoisted.

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

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

A

Both are comparison operators. The difference between both the operators is that,“==” is used to compare values whereas, “ === “ is used to compare both value and types.

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

What is IMPLICIT TYPE COERCION?

A

The automatic conversion of value from one data type to another. Takes place when the operands of an expression are different data types.

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

What values are FALSY in JS? (there are 8)

A

false, 0, 0n, -0, “”, null, undefined and NaN

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

What TYPE of language is JS

A

JS is a dynamically (loosely) typed language. The type of variable is checked on RUN-TIME vs COMPILE-TIME with statically typed programs. Also, variables can change their data type if reassigned.

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

What is NaN?

A

Means “not-a-number”. Can be used to check if a value is not a legal number by using isNaN( ) and passing the value as an argument.

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

What is passed by VALUE vs. passed by REFERENCE?

A
Primitive data types are passed by value and non-primitive are passed by reference. This means that when a primitive variable is assigned to another primitive variable, it will create a copy of that value:
var x = 23;
var y = x;
var x = 24; // var y is still equal to 23 because it is made as a separate value.

With non-primitive data-types (objects), the assignment operator is creating a new variable name for the object, where the new variable name points to the same object it was assigned to, and not a copy:

var obj1 = { a: 1, b: 2 };
var obj2 = obj1;
obj1.c = 3 // obj1 also has the c: 3 property because it's referring to the same object.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is an IMMEDIATELY INVOKED FUNCTION in JS?

A

Known also as IFFE/IFFY (“iffy”). A common JavaScript pattern that executes a function instantly after it’s defined. Developers primarily use this pattern to ensure variables are only accessible within the scope of the defined function. Created by enclosing the function with parentheses and adding parenthesis after the closed parenthesis of the function.

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

What are HIGHER ORDER FUNCTIONS?

A

Functions that operate on other functions, either by taking them as arguments or by returning them, are called higher-order functions.

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

Explain the THIS keyword

A

The “this” keyword refers to the object that the function is a property of. If the function is not part of an object, then ‘this’ refers to the global object, or Window object.

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

Explain .call( );

A

The .call( ) method is actually what is being used when you invoke a function. So, when you add the parenthesis after a function to call it, this is actually the method that is used behind the scenes.

.call( ) can also be used if you want to use a property from another object on some other object that doesn’t have that property:

const player1 = { name: Bob, battery: 70, chargeBattery: ( ) => this.battery = 100 };
const player2 = { name: Sue, battery: 80 };
player1.chargeBattery.call(player2) // This says take the chargeBattery function from player1 and apply it to player2

.call( ) can also take arguments after the object you are calling.

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

Explain .apply( );

A

The apply method is similar to the .call( ) method, except that if it takes arguments in addition to the called object, those arguments are passed as an array of values.

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

Explain .bind( );

A

The bind() method creates a new function that, when called, has its ‘this’ keyword set to the provided argument (which is an object):

const person1 = { name: 'Sam' }
const person2 = { name: 'Saul' }
function sayHi( ) { console.log( this.name + ' says hi!') }
const samSaysHi = sayHi.bind(person1);
samSaysHi( ) // returns 'Sam says hi!' because the function sayHi was bound to the person1 object and initialized as the function samSaysHi. Then, it was called.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is CURRYING?

A

Currying takes a function that has more than one parameter and breaks it into many unary functions (or functions that have only one parameter). It then calls each function with the separate parameters one at a time.

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 can be global, local/functional, or block. It refers to where a variable or function can be accessed within your code. A variable/function that is globally scoped can be accessed anywhere inside the code. Local is accessed within a function, and block is within curly braces.

Scope chain refers to how a variable is first looked for within the block scope, then local scope, then global to find the value.

17
Q

Explain CLOSURES

A

Closures is an ability of a function to remember the variables and functions that are declared in its outer scope.

18
Q

What are OBJECT PROTOTYPES?

A

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

19
Q

What are CALLBACKS?

A

A callback is a function that gets executed after another function is executed. It is a function that is passed as the argument of another function.

20
Q

What is MEMOIZATION

A

Memoization is a form of caching where the return value of a function is cached based on its parameters. If the parameter of that function is not changed, the cached version of the function is returned.

21
Q

What is RECURSION?

A

Recursion is a technique to iterate over an operation by having a function call itself repeatedly until it arrives at a result. The condition of the result is stated first to check if it is true, and if not, it continues to iterate over the the operation:

function add(number) {
  if (number <= 0) {
    return 0;
  } else {
    return number + add(number - 1);
  }
}
add(3) // returns 3 + 2 + 1 + 0 = 6
22
Q

What is the DOM?

A

“Document Object Model” - It’s a programming interface for HTML and XML documents. When the browser tries to render an HTML document, it creates an object based on the HTML document called the DOM. We can “traverse the DOM” to manipulate or change elements.

23
Q

What are CONSTRUCTOR FUNCTIONS?

A

Constructor functions are used to make objects in JS. They can be used to create multiple objects having similar properties and methods. They use Pascal case (first word of variable name is capital). A new object is created by assigning to a variable name and using the ‘new’ keyword.

24
Q

What are ARROW FUNCTIONS?

A

Introduced in ES6. Shorter syntax that can only be written as expressions. The this keyword inside an arrow function does not refer to the object calling it. It rather inherits its value from the parent scope. If that isn’t another function, then it’s the window object.

25
Q

What are the differences between VAR, LET and CONST?

A

The var keyword was used to declare variables before ES6. It is function scoped. The let keyword is similar to var in that the value can be reassigned, however it is block scoped instead of function scoped. This means a variable declared with let inside a block (or curly braces) are not accessible outside its scope, whereas a variable declared with var is accessible outside a block. The const variable has the same block scope as let, however it can only be assigned once.

26
Q

What are REST PARAMETERS and the SPREAD OPERATOR?

A

The sytax of both is the same (…), however they serve different purposes. When used before the parameter inside a function (used as a rest parameter), you can create a function that takes a varied amount of arguments. Rest params should always be used as the last argument if there are any preceding it. When used as the spread operator, it spreads out the values inside of an array or object. It basically takes the value out of the object and treats them as separate values. You can use it to clone or merge objects/arrays.

27
Q

What are PROMISES?

A

A way to handle asynchronous operations. Promise has four states: pending, fulfilled, rejected, settled. Initially has a pending, or intermediate state. Basically says that it will get back to you with the return value as soon as it is settled. It is settled by being fulfilled or rejected. We can chain .then and .catch to a promise to handle the resolved or rejected state of a promise once it is settled.

28
Q

What are CLASSES?

A

Syntactic sugar for constructor functions. Classes are not hoisted, unlike function declarations. A class can inherit properties and methods from other classes by using the extend keyword. All the syntaxes inside the class must follow the strict mode(‘use strict’) of javascript. Error will be thrown if the strict mode rules are not followed.

29
Q

What is DESTRUCTURING?

A

The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables:

ARRAYS:
const array = [1, 2, 3, 4];
const [a, b] = array;
console.log(a, b) // returns 1 2
OBJECTS:
const obj = { a: 1, b: 2 };
const { a, b } = obj;
console.log(a, b) // returns 1 2
30
Q

Explain REST

A

Stands for Representational State Transfer. It is basically a set of guidelines for how a client and server should communicate and perform CRUD (Create, Read, Update, Delete) operations on a given resource. Guided by these principals (from Wikipedia):

  • Client-server separation
  • Statelessness
  • Cacheability
  • Layered System
  • Uniform Interface