Exam Specific Questions Flashcards
How is a variable’s scope determine?
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 is a block scope defined?
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.
What is an undeclared variable? What is a notable feature of undeclared variables?
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.
What are the 3 broad rules of variable scope?
- A variable declared outside of a block is accessible inside the block.
- Inner scope variables cannot be accessed in outer scope.
- 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.
What are the 2 rules specific to function scope?
- Peer function scopes do not conflict.
i. e. Two different functions can have the same variable names within its scope - 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.
What are the two types of values all JavaScript data types can be categorized into?
Every value in JavaScript is either a primitive value or an object.
What are the 7 primitive data types?
String, Boolean, Number, Null, Undefined
Symbol and BigInt were introduced in ES6 but are less commonly used.
What is the difference between null and undefined?
Both represent an absence in value, but null is an intention absence usually assigned by design.
What is the key feature of primitive data types?
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.
What is the difference between a non-mutating and a mutating operation?
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
What are some common string methods? What are their return values?
.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
What are some common array methods? What are their return values?
.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
What are considered objects? What is the key feature of objects?
Objects include: Simple objects Arrays Date objects Functions
Objects are mutable, you can change part of an object.
How do you access keys and values in an object as an array?
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.
What is an object’s inheritance?
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 do you iterate over an object’s keys and values? How do you exclude iterating over inherited properties?
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 do you merge the properties of two different objects?
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.
What are sparse arrays?
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> ]
Why are arrays considered objects?
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']
Explain the difference between a pass by reference and a pass by value? Which strategy does JavaScript utilize?
‘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)
When are variables considered pointers aka references? What happens when variables that are pointers are reassigned?
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.
What is the key difference between returning a value and printing/displaying it?
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.