JavaScript Random 1 Flashcards

1
Q

What is pass by value?

A

In Pass by Value, Functions are called by directly passing the value of the variable as the argument. Changing the argument inside the function doesn’t affect the variable passed from outside the function.

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

What is pass by reference?

A

In Pass by Reference, Functions are called by directly passing the reference/address of the variable as the argument. Changing the argument inside the function affects the variable passed from outside the function.

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

What type of variables are passed by value in JavaScript?

A

JavaScript is always pass by value for String or Number so changing the value of the variable never changes the underlying primitive.

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

What type of variables are pass by reference in JavaScript?

A

In JavaScript objects and arrays follow pass by reference.

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

How do you access an objects property using a variable?

A

Use bracket notation.

const myObject = {
    name: "Thomas Mann", 
    age: "32"
}
const prop="name";
console.log( myObject[prop] );
// Output "Thomas Mann"
const prop="age";
console.log( myObject[prop] );
// Output "32"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does Array.prototype.filter() do?

A

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

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

How would you use filter to remove words from an array that are 4 letters long or less?

A

const arr = [‘alphabet’, ‘beef’, ‘chardonay’];

const filteredArray = arr.filter((word)=> word.length>4);
// If the test on the element returns true, the element goes in the new array
console.log(filteredArray);
// Output ["alphabet","chardonay"]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What phrase did I come up with that helps me remember how filter works?

A

If the filters test returns true, the current element goes in the new array.

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

How do you prevent an object’s properties from being modified?

A

You use the Object.freeze( ) method.

const obj = {
  prop: 42
};

Object.freeze(obj);

obj.prop = 33;
// Throws an error in strict mode
console.log(obj.prop);
// expected output: 42
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Will trying to modify a object that has been frozen throw an error?

A

Sometimes.

Nothing can be added to or removed from the properties set of a frozen object. Any attempt to do so will fail, either silently or by throwing aTypeErrorexception (most commonly, but not exclusively, when instrict mode).

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

What is a shallow freeze?

A

The result of callingObject.freeze(object)only applies to the immediate properties ofobjectitself and will prevent future property addition, removal or value re-assignment operationsonlyonobject. If the value of those properties are objects themselves, those objects are not frozen and may be the target of property addition, removal or value re-assignment operations.

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

Is this a deep freeze?

Object.freeze(object)

A

No this is a shallow freeze.

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

How do you do a deep freeze?

A

You have to write a function to recursively go through an object’s properties looking for object’s or arrays that also need freezing.

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

Are functions in JavaScript Objects?

A

Yes!

In JavaScript, functions are first-class objects, because they can have properties and methods just like any other object. What distinguishes them from other objects is that functions can be called. In brief, they are Function objects.

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

What does this highlight?

function myFunc(){
  let a=2;
};

console.log(myFunc.name);

A

That functions are object’s in JavaScript. Here we are accessing the name property of the function object.

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

What can JavaScript statements be composed of ?

A

Values, Operators, Expressions, Keywords, and Comments.

17
Q

What separates JavaScript Statements?

A

Semicolons separate JavaScript statements.

18
Q

A computer program is a list of “instructions” to be “executed” by a computer.
In JavaScript, these programming instructions are called what?

A

statements

19
Q

What is a callback?

A

A JavaScript callback is a function which is to be executed after another function has finished execution. A more formal definition would be - Any function that is passed as an argument to another function so that it can be executed in that other function is called a callback function.