Exam Specific Questions Flashcards

1
Q

How is a variable’s scope determine?

A

A variable’s scope determines where it is available in a program. The location where you declare a variable determines its scope.

You can declare a variable with global scope or local scope.

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

How is a block scope defined?

A
A block is a set of statements and expressions between a set of curtly braces { }. 
E.g. if statements, for and while loops
Note: Object literals and class definitions are not blocks 

Let and const variables have block scope, which means if declared within a block, they are only accessible within that block.

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

What is an undeclared variable? What is a notable feature of undeclared variables?

A

An undeclared variable is a variable not declared with let, const, or var.

E.g. variable = 123

Undeclared variables have global scope and ignore function/block scope entirely.

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

What are the 3 broad rules of variable scope?

A
  1. A variable declared outside of a block is accessible inside the block.
  2. Inner scope variables cannot be accessed in outer scope.
  3. Inner scope variables will overshadow outer scope variables. i.e. once the inner scope has the same name as an outer scope variable, the outer variable will not be accessible inside the scope.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the 2 rules specific to function scope?

A
  1. Peer function scopes do not conflict.
    i. e. Two different functions can have the same variable names within its scope
  2. Nested functions have their own variable scope.
    Same levels apply where the innermost function can access outer scopes, but outer scopes cannot access inner function scopes.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the two types of values all JavaScript data types can be categorized into?

A

Every value in JavaScript is either a primitive value or an object.

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

What are the 7 primitive data types?

A

String, Boolean, Number, Null, Undefined

Symbol and BigInt were introduced in ES6 but are less commonly used.

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

What is the difference between null and undefined?

A

Both represent an absence in value, but null is an intention absence usually assigned by design.

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

What is the key feature of primitive data types?

A

Primitive data types are also known as atomic or indivisible data types since they are immutable.

They can only be reassigned if declared using let or var, but not changed.

Any operation performed on a primitive value will return a new primitive value.

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

What is the difference between a non-mutating and a mutating operation?

A

A non-mutating operation preserves the previous value of the data passed in.

A mutating operation will change the previous value of the data passed in.

Primitive data type methods are non-mutating since primitive values are immutable.

Mutation is a concern for certain array and object methods since passed in arrays/objects have the chance of being mutated. i.e. sort, push, pop

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

What are some common string methods? What are their return values?

A
.includes => true/false
.concat => new string
.split => new array
.trim => new string
.toUpperCase / .toLowerCase => new string
.charAt => character located at index
.charCodeAt => UTF-16 code of character at index
.repeat => new string
.endsWith / .startsWith => true/false
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are some common array methods? What are their return values?

A

.forEach => undefined
.map => new array
.filter => new array of elements that satisfy callback
.find => first element in the array that satisfy callback
.sort => original array, mutated
.includes => true / false
.reduce => single accumulated output value

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

What are considered objects? What is the key feature of objects?

A
Objects include:
Simple objects
Arrays
Date objects
Functions

Objects are mutable, you can change part of an object.

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

How do you access keys and values in an object as an array?

A

Object.keys() and Object.values() methods allow you to access keys and values as separate arrays.

Object.entries() will return a nested array of key/value pairs.

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

What is an object’s inheritance?

A

A child object can inherit properties from another parent object.

Object.create() is a simple way to create a new object that inherits from an existing object.

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

How do you iterate over an object’s keys and values? How do you exclude iterating over inherited properties?

A

You can loop over an object’s keys and values with a for/in loop. A regular for/in loop with iterate over all inherited properties as well.

To limit iteration to an object’s own properties, use the hasOwnProperty() method (which will return true if not inherited)

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

How do you merge the properties of two different objects?

A

Object.assign() method allows you to merge two or more objects. It allows you to combine key-value pairs into a single object.

NOTE: Object.assign will mutate the target object (first argument passed in). To create a new object and avoid mutation, use an empty object as the first argument.

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

What are sparse arrays?

A

Arrays may have gaps where there are missing elements aka elements that have never been assigned a value.

Accessing these gap elements by index will return undefined, but NOTE this does not mean the value is undefined. It is not set at all.

E.g. The following is either called a 3-element array whose values are missing OR an empty array with a length of 3:

> let array = [ ];
array.length = 3;
console.log(array) // => [ <3 empty items> ]

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

Why are arrays considered objects?

A

Arrays are also objects, but with indexes (non-negative integer) as their property key.

It is possible for an array to have properties whose name is not a non-negative integer.

E.g. 
> let array = [1, 2];
> array[-1] = 3;
> array['foo'] = 'boo';
> console.log(array)    // => [1, 2, '-1': 3, foo: 'boo']
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Explain the difference between a pass by reference and a pass by value? Which strategy does JavaScript utilize?

A

‘Pass by reference’ and ‘Pass by value’ are both strategies that describe the affect of function arguments.

Pass by value describes a variable that is passed in as a function argument and the function can’t do anything that mutates the variable. This is the case for JavaScript’s primitive data types.

Pass by reference describes a variable being passed in as a function argument and the function call can affect the original data value. This is the case for arrays and objects, which have the chance of being mutated by certain methods (e.g. pop, push, shift)

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

When are variables considered pointers aka references? What happens when variables that are pointers are reassigned?

A

JavaScript uses pointers for non-primitive values like arrays and objects.

Non-primitive values are stored in a region of memory (aka heap) that is pointed or referred to when the data retrieved.

Therefore when a variable is assigned the value of another variable that is an array or object, the value is pointing to the same region in memory. Mutating one variable will also mutate the other.

However, reassigning one of the variables will not mutate the other. Reassignment will cause the reassigned variable to point to a different region in memory.

It is important to note that reassigning a single element or property in an array or object WILL mutate all variables referencing the same data in memory.

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

What is the key difference between returning a value and printing/displaying it?

A

Console.log is a method that tells JavaScript to print/display something to the console. In node, that is your screen. In the browser, that is your developer tool.

The return value for console.log, however, will always be undefined because the expression evaluates to nothing.

A return value is the evaluated value of an expression. Expressions are anything JS can evaluate to value, even if it is undefined or null.

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

What are the 6 falsy values? What are truthy values?

A
The 6 falsy values are:
False
0 (zero)
NaN
"" (empty string)
undefined
null

Everything that is not a falsy value, is a truthy value.

24
Q

What is the correct method to describe a truthy or falsy value?

A

When discussing a truthy/falsy expression, it is recommended to use terms such as “evaluates to true” or “is truthy” instead of “is true” or “equals to true”.

25
Q

What is the difference between a variable declaration and a variable assignment?

A

A variable declaration is a STATEMENT that asks JavaScript to reserve space for a variable with a particular name.

An assignment is when a variable (or identifier) is initialized with a value using the ‘=’ assignment operator.

Note: Variable declarations have a return value of undefined, even if there is also an assignment. A pure assignment without a declaration, will have a return value of the value on the right hand side of the ‘=’ operator.

26
Q

What are the comparison operators? What are their return values?

A

Comparison operators include:

> , =, <=, ==, ===, !=, !==

All comparison operators have a boolean return value.

27
Q

What is the difference between a strictly equal ‘===’ and a loosely equal ‘==’ operator?

A

A strictly equal operator returns true when both operands have the same value AND data type.

A loosely equal operator returns true when both operands have the same value, but not necessarily the same data type.

28
Q

What will NaN === NaN return?

A

NaN === NaN OR
NaN == NaN

will always return false

29
Q

What are the three logical operators?

A

&& (AND), | | (OR), ! (NOT)

&& returns true when both operands are true.

! will negate the operand and return the opposite boolean value

| returns true when one of the two operands is true.

30
Q

How do logical operators short circuit?

A

Using the && operator, if the first operand is false then expression will terminate and the second operand will not run.

Using the | | operator, if the first operand is true, then the expression will terminate and the second operand will not run.

Note: The return value of the two logical operators (&&, | |) is always the value of the operand evaluated last.

31
Q

What is the order of operator precedence?

A
  1. Comparison: <=, , >=
  2. Equality: ===, ==, !==, !=
  3. Logical AND &&
  4. Logical OR | |
32
Q

What is the difference between implicit and explicit coercion?

A

Implicit coercion describes JavaScript’s default process of changing one of two data types to match when performing an operation involving two different data types.

Explicit coercion is a programmer’s intentional alteration of a data type using Javascript’s built-in functions.

You should always rely on explicit coercion when working with different data types!

33
Q

What data types do JavaScript implicitly coerce when using template literals?

A

Inside template literals, JS coerces all expressions wrapped in ${ } to string values.

34
Q

What happens when you use the == loosely equal operator to compare a string and a number?

A

When using the == operator to compare a string and a number, JavaScript will implicitly coerce the string into the number.

string == number
string becomes a number. If it is not a numeric value, the value will be NaN

35
Q

What happens when you use the == loosely equal operator to compare a string OR number with a boolean?

A

string/number == boolean
JavaScript will implicitly coerce the boolean value into its number value.
true = 1, false = 0

e.g.
‘1’ == true // => true because true is coerced to 1

36
Q

What will be the return value when comparing ‘null’ and ‘undefined’ with a == loosely equal operator? What about with a === strictly equal operator?

A

null == undefined // => true, coerced to zeros

nul === undefined // => false

37
Q

What will be the return value when comparing an empty array with an empty string with a loosely equal operator? What about zero or an empty object?

A

[] == “” // => true, both coerced to numbers (zeros)
[] == 0 // => true, empty array coerced to number (zero)
[4] == 4 // => true, array with single element coerced to number

”” == 0 // => true, empty string coerced to number (zero)

[] == {} // => false, two objects (even empties) are never equal unless pointing to the same point in memory

38
Q

When using the + operator and ONE of the operands is a string, what will happen to the other operand?

A

When using the + operator, if one of the operands is a string then the other operand will be implicitly coerced by JS into a string.

E.g.
> ‘number’ + 1 // = > ‘number 1’
> “” + [1, 2, 3] // => ‘1,2,3’
> “” + {} // = > ‘[object Object]’

39
Q

When using the + operator, what will happen when the operands are primitive data types other than strings and undefined (either numbers, booleans, or nulls)?

What if undefined is one of the operands?

A

When using the + operator with numbers, booleans, and nulls, both operands will be converted to numbers and added together.

E.g.
> true + false // => 1 + 0 = 1
> 7 + null // => 7+ 0 = 7

Undefined with the + will always return NaN
> undefined + null // => NaN
> undefined + 8 // => NaN

40
Q

When using the + operator, what will happen when one of the operands is an object?

A

Both operands will be converted into a string and concatenated together.

The object (empty or filled) becomes the string ‘[object Object]’.

E.g.
> {foo: 1} + 8 // => ‘[object Object]8’
> {foo: 1} + undefined // => ‘[object Object]undefined’

41
Q

What happens when you use relational operators (e.g. >, =, <=) to compare two different data types?

A

All non-numbers (strings, boolean, null, undefined) will be converted to numbers.
Note: Undefined’s number form is NaN.

E.g.
> true > null // => true since 1 > 0
> 123 > ‘a’ // => false since 123 is not greater than NaN
> 123 > undefined // => false since 123 is not greater than NaN

42
Q

What happens when you use relational operators (e.g. >, =, <=) to compare two strings?

A

When both operands are strings, they are compared in lexicographic (abc) order.

e.g. ‘b’ > ‘a’

43
Q

What are some ways to explicitly convert non-number data types (e.g. strings, arrays) to numbers?

A

Number() method will convert numeric strings, booleans, null, single digit arrays to their numeric value.
Non-numeric strings, undefined, empty objects, filled arrays will return NaN.
Empty strings, empty arrays, whitespace strings, and false will return 0 (zero).

parseInt() method functions similarly to Number(). The key difference is that it will convert a string to a number as long as it begins with a digit. (e.g. ‘12oz’)

The unary + operator will also change a value to a number and works exactly the same as Number().

44
Q

What are some ways to explicitly convert non-string data types to strings?

A

toString() or String() will both convert to string.

String() will also convert undefined and null to strings whereas toString() will have result in an error.

45
Q

What is the difference between a function definition and function invocation?

A

Function definition includes a function’s parameters and the code within the function body. A function definition will not run/execute until it is invoked.

Function invocation (aka calling the function) is writing the function name and passing in argument(s) within a set of parentheses. Upon invocation, the function will execute the code within the function body with the passed in arguments as its parameters.

46
Q

What are function declarations? What is the syntax?

A

Function declaration is a way of writing functions using the ‘function’ key word, curly braces, and an explicit return statement. If there is no return statement, the function will return undefined.

47
Q

What are function expressions? What is the syntax?

A

Function expressions are function definitions saved in a variable (let, const, var).

E.g. let greetPeople = function () {
       console.log('Good morning');
        };

NOTE: Wrapping a function declaration in a set of parentheses will create a function expression.

48
Q

Are functions returned from a higher order function considered a function declaration or function expression?

A

Functions returns from a higher order function are considered function expressions.

E.g. greeter function below is a function expression 
 > function makeGreeter(name) {
 >   return function greeter() {
 >        console.log(`Hello ${name}`);
 >   }
 > }
49
Q

What is the key feature difference between function declarations and function expressions?

A

Function declarations can be invoked BEFORE it is declared in the code because they are hoisted to the top when the program is executed.

Function expressions cannot be invoked before it appears in a program. Calling a function expression before it is written in the program will result in an error.

50
Q

What is an arrow function? What is the syntax?

A

Arrow functions are function expressions that do not use the function keyword.

The return statement and curly braces can be omitted if the function body contains only ONE expression.

If there are multiple lines/expressions in the function body, then return statement and curly braces are needed.

Parentheses can be omitted if there is only one argument.

51
Q

What is the implicit return value for functions?

A

By default, a function’s return value is undefined.

When a return statement is used within the function definition, that specific value will be returned to the location where it was called/invoked.

52
Q

Describe higher-order functions in JavaScript.

A

Higher-order functions are functions that take functions as an argument or return a function.

It doesn’t have to have function definitions within their body to be considered higher-order.

It can return any function, including a function that is another higher-order function.

53
Q

What are considered first-class values in JavaScript?

A

First-class values are values that can be passed to functions as arguments or returned by those functions.

All JS functions are first-class functions i.e. all functions can be passed around as values.

Functions passed to other functions are often called callbacks.

It doesn’t have to accept another function as an argument or return a function to be considered first-class.

There is no limit to the number of functions that can be passed as arguments to a first-class function.

54
Q

Describe the 6 possible function side effects.

A
  1. It changes the value of a variable defined in the outer scope.
  2. It changes the state or content of an array or object that is assigned to a variable in the outer scope.
  3. It changes the state or content of an array or object passed to the function as an argument.
  4. It uses a variable from the outer scope that is not a constant.
  5. It reads or writes output from any source
    e. g. using readline-sync to define a variable, console logging
  6. It calls another function that has a side effect
55
Q

What are the 3 characteristics of pure functions?

A
  1. Always return a value that is dependent on its arguments
  2. Have no side effects
  3. Given the same arguments, the function always return the same value.