JS109 Flashcards

1
Q

What is the broader term for

Variables declared with let and var
Constants declared with const
Properties of the global object
Function names
Function parameters
Class names

A

Identifier

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

Are object keys ever identifiers?

A

No, not all of them.
But all of them are on the global object

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

What happens when you do a variable declaration?

A

Asks the JavaScript engine to reserve space for a variable with a particular name.

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

What is the name for =

A

assignment operator

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

assignment operator

A

=

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

let function = function() {
??what is this space??
}

A

the function body

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

Difference between function call and invocation?

A

Same
But “invocation” and “invoking” are easier to distinguish noun and verb.

You invoke a function to call it or write a function invocation that will be called when the program runs

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

Are parameters just variables

A

Yes basically

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

A function that always return boolean are called…

A

a predicate

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

What is a predicate?

A

predicates are functions that always return boolean

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

All JavaScript function calls evaluate to a value t or f

A

T
If it’s not defined it returns undefined

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

When a method mutates the object it was called on it’s called

A

mutates the caller

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

What is it called when you pass a function as the argument into a function?
like this:
add(subtract(80, 10), times(subtract(20, 6), add(30, 5)));

A

function composition

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

What is this operator called %

A

remainder operator
modulo operation

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

You cannot invoke a function expression before it is declared t or f

A

T

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

What does it mean to be first class functions? (2 things)

A

They are treated like values and can be assigned to variables. They can also be parsed into other functions as arguments

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

Describe how the callstack works:
1 function first() {
2 console.log(“first function”);
3 }
4
5 function second() {
6 first();
7 console.log(“second function”);
8 }
9
10 second();

A

https://launchschool.com/books/javascript/read/functions#callstack
Push to the stack
Pop from the stack
Stack frame

The call stack helps JavaScript keep track of what function is executing as well as where execution should resume when the function returns.

main, initial stack frame called the main function

main

Program execution reaches a function invocation, and updates the main stack frame with the current program location. So it knows where to come back to when it’s done executing this function.

Then a new stack frame is pushed onto the stack for this new function:

second
main: line 10

Main is inaccessible and dormant until second completes.
The program reaches another function invocation. The second frame is updated with the line so JS knows where to resume execution later.

first
second: line 6
main: line 10

Etc. When it comes back, it starts executing again immediately after the invocation.

second: line 6
main: line 10

second: line 7
main: line 10

main:line 10

then an empty stack when the program finishes.

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

What is one level of the stack called?

A

Stack frame

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

What is the conditional part of a loop called?

A

A clause

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

Another name for strict equality operator and non-strict equality operator

A

identity operator
loose equality operator

21
Q

What kind of operators are these:
||
&&
!

A

logical operators

22
Q

What’s this called
undefined ?? null
How does it differ from ||

A

nullish coalescing operator

|| will also return left operator if falsy

0 ?? 2
will return 0
0 || 2
will return 2

23
Q

When should the ternary operator be used?

A

When choosing between return 2 values
Not when choosing between 2 actions

24
Q

What kind of statement are break, continue, return?

A

jump statements

25
Q

What are these operators called?
++

A

increment
decrement

26
Q

Methods like forEach have a special term, what is it?

A

array looping abstractions

27
Q

What is the part of a recursive function that ends the recursion?

A

baseline condition

28
Q

What is an adjective for primitive immutable values?

A

atomic
base unit
Even something like 0 + 0 evaluates to a new value of 0.

29
Q

Kinds of errors
Syntax
Reference
Type

A

Syntax- like a grammar mistake. The specific syntax is wrong and JS doesn’t know what to do

Reference- variable used when not declared or can’t be found

Type error- Short for data type error. When a value is used outside the scope of it’s data type.
ex:
let num = 15;
console.log(num.split(“”));

30
Q

What happens when an object has 2 keys with the same string?

A

The second one will override the first

31
Q

What is a template literal? (allows 2 things)

A

literals delimited with backtick (`) characters, allowing for multi-line strings (usually new lines in a string literal automatic end the line), string interpolation with embedded expressions,

32
Q

What does array.reverse return, does it mutate?

A

Mutates
Returns reference to array

33
Q

What does array.pop return, does it mutate?

A

Mutates, removes that element from array
returns that element

34
Q

What does array.push return, does it mutate?

A

Mutates, add the argument to the end of the array
returns the new length of the array

35
Q

Describe the ternary operator

A

It’s basically an alternative to an if else statement.
It takes 3 operands, the only operator to do so.
A condition,
followed by a question mark then an expression to execute if the condition is truthy follow by a colon, then a expression to execute if the condition is falsy.
Can chain it for a chained conditionals.

36
Q

demonstrate nested ternany operatorr

A

https://medium.com/javascript-scene/nested-ternaries-are-great-361bddd0f340

37
Q

can you exclude any part of the ternany operator

A

no
false ? console.log(‘a’)
will result in a syntax error

38
Q

What does require do?
Can you write to the JSON file?
Can you edit the JSON while using it?

A

It allows access to modules which allows splitting the code into different files.
These different files have different scopes (unlike using script in web)

No. require caches the result. You can make changes and those changes will be available later in code, but it will not effect the original JSON file.

39
Q

Difference between block, local, and function scope? and define
What scopes do var, let and const have?
What are the 5 scoping rules

A

Global: available everywhere
Local scope: not everywhere. Only between curly braces. There are 2 kinds of local scope. Block scope and function scope.
They each have the same rules, but one describes local scoping rules for functions, and blocks the rest
This is because var is function scoped.

var is globally scoped outside of functions. Function scoped in a function.
let and const are block scoped. So they are globally scoped when outside of all

Scoping rules
1
outer scope variables accessible within inner scope
2
inner scope variables not accessible within outer scope
3
peer scopes do not conflict
4
nested blocks/functions have their own variable scope
5
inner scope variables can shadow outer scope

40
Q

What is a side effect? What is a pure function?

What is output compared to return?

A

A side effect is when a function does something that you can’t see (read, write, error, mutate, reassign)
A pure function has the same output for the same input. No side effects. It doesn’t depend on something else.

It reassigns any non-local variable. Reassigning a variable in the outer scope would be a side effect.

It mutates the value of any object referenced by a non-local variable. Mutating an array or object argument, for instance, would be a side effect.

It reads from or writes to a file, network connection, browser, or the system hardware. Side effects like this include writing to the console log and reading input from the terminal.

It raises an exception without handling it.

(possibly incorrect) A return is a what a function will evaluate to.
Output is what the program gives back to us. (a keystroke in a text box, outputs the key that we stroke).
“Any information processed by and sent out from a computer or other electronic device is considered output. An example of output is anything viewed on your computer monitor screen, such as the words you type on your keyboard. Without some type of output that a human could see, feel, or hear, a human could not interact with the computer.”

41
Q

Expression vs statement

A

Expression is anything that evaluates to a value that can be captured and used in subsequence code.

A statement is any syntactic unit of code that expresses an action for the computer to perform. A statement is not an expression. It’s return cannot be used in subsequent code (will throw a syntax error)

This includes:
variable, function, and class declarations
loops and if statements
return and break statements
assignments: a = 3;
standalone expressions: console.log(“Hello”);

42
Q

Pass by value vs pass by reference and give example

A

value
function() {return 6}
function(6)

Reference
function() {return []}
function([])

43
Q

What does Math.pow return?

A

the first argument to the power of the second

44
Q

exceptions: throwing and catching (incomplete)

A

https://www.google.com/search?q=exceptions%3A+throwing+and+catching+js&oq=exceptions%3A+throwing+and+catching+js&aqs=chrome..69i57j69i58.1037j0j7&sourceid=chrome&ie=UTF-8

45
Q

Best way to prevent errors

A

Look through, try edge cases, create guard clauses

46
Q

If you don’t want to plan for every permuation of a situations, how can you catch errors
What can be omitted?

A

try {
// Do something that might fail here and throw an exception.
} catch (error) {
// This code only runs if something in the try clause throws an exception.
// “error” contains the exception object.
} finally {
// This code always runs even if the above code throws an exception.
}

Finally can be ommitted always
Catch can be ommitted if there’s no error
You can’t ommit both

47
Q

What argument will go into the parameter in a catch function

A

The error object

48
Q

What is function composition

A

When you pass the function call as an argument in another function

49
Q

What is a template literal? (allows 2 things, also a third)

A

literals delimited with backtick (`) characters,
allowing for multi-line strings,
string interpolation with embedded expressions,

and special constructs called tagged templates.