Codesmith Technical Interview
Closure
A closure in JavaScript is a function that retains access to variables from its outer scope even after the outer function has finished executing. It allows the function to "remember" and access those variables when it is invoked later.
Interpreter Phases (2)
Allocation: -declarations stored in memory, hoisted to top of scopes. Execution: -Values are assigned, expressions are evaluated, invocations are executed.
Javascript, the language is...
High-level ● Interpreted ● Dynamically typed ● Multi-Paradigm, Object-oriented, Functional ● Memory-managed ● Garbage collected
pass by reference
Pass by Reference: When a language uses pass by reference, instead of passing a copy of the value, a reference or memory address of the variable is passed to the function. Any changes made to the parameter within the function affect the original variable outside the function because they both refer to the same memory location.
Two items needed to begin executing code...
1) Execution context 2) call stack: Tells the interpreter which execution context is in control of the thread of execution -It is a Stack (data structure following the LIFO model, opposite of a Queue) Each execution context is represented by a Stack Frame. When a function is invoked, a new frame is pushed to the call stack. When a function returns, its frame is popped off the call stack, and the top most frame takes control of the interpreter.
Callback function
A function passed as a parameter to another function in order to be called later.
Side Effect
A side effect is any application state change that is or remains observable outside a function invocation. Mutations to argument values, and console.log(which can help us)
functional programming
A style of building the structure and elements as computations as an evaluation of mathematical functions ● Consists of only pure functions ● Avoids changing state or mutable data
Javascript's two distinct runtimes
Browser (client side) NodeJS (server side)
Why Objects?
DATA + FUNCTIONALITY TOGETHER
Dot vs Bracket notation
Dot: retrieving a specific key name or adding it. Bracket: you need to use a variable or string type to represent the key name
hoisting
Hoisting is a JavaScript behavior where variable and function declarations are moved to the top of their containing scope during the execution phase. Variables declared with var return undefined when referenced before their values are assigned. Variables declared with let and const throw a ReferenceError when referenced before their values are assigned.
Javascript's execution is...
Its execution is... ● Single-threaded ● Synchronous ● Blocking
map method
Native array method that iterates through every element in an array - creating a new array with values that have been modified by the callback function example: var timesFour = oldArray.map(function(val){ return val * 4; });
Parameter vs Argument
Parameters contain types and are part of the function declaration. Arguments do not contain types and are part of the function invocation.
pass by value
Pass by Value: When a language uses pass by value, a copy of the value of the variable is passed to the function. Any changes made to the parameter within the function do not affect the original variable outside the function. Essentially, a new memory location is created for the parameter within the function.
Pure Functions
Predictable outcomes given the same input; No side effects
Methods
Properties on Objects that are functions
Prototypical Inheritance
Prototypical inheritance in JavaScript is a mechanism where objects can inherit properties and methods from other objects called prototypes. When a property or method is accessed on an object, if it doesn't exist in the object itself, JavaScript looks up the prototype chain to find it in higher-level prototypes, allowing for property sharing and hierarchical relationships between objects.
Recursion
Recursion is a programming technique where a function calls itself to solve a problem by breaking it down into smaller, similar subproblems. It involves a base case that terminates the recursive calls and a recursive case that reduces the problem towards the base case.
Object Literal
creating properties, or key-value pairs directly into a literal object.
Higher-order functions
functions that can accept other functions as arguments, and return functions as values, or call another functions within its code block
Object Literal --> Factory Function --> Constructor Function --> Classes
make sure you know when to use this and new.
new keyword
Used to create a new instance of an object. Returns the object out
reduce method
used to iterate through an array to condense it into one value. you pass in a callback whose arguments are an accumulator, and a current value. Reduce stores the result of the callback(curr) as the new acc. and so on and so on.
Object Oriented Programming (OOP)
● Relies on the concept of classes and objects. ● A style that structures a software program into simple, reusable pieces of code blueprints, which are used to create instances of objects
ES6 Rest
allows us to represent an indefinite number of arguments as an array
.forEach() method
calls a provided function once for each element in an array, in order.
scope
Scope in JavaScript refers to how visible and accessible your variables, functions, and objects are in a particular part of your code. Imagine scope as a container that holds your variables and functions. It determines where these variables and functions are accessible and where they are not. There are two main types of scope in JavaScript: Global Scope: Variables declared outside of any function or block have global scope. They are accessible from anywhere in your code, including inside functions and blocks. Local Scope: Variables declared inside a function or block have local scope. They are only accessible within the function or block where they are defined. They are not visible outside of that specific scope. Local scope can be nested within ot4rrher local scopes, creating a hierarchy. In such cases, inner scopes have access to variables in their outer scopes, but not vice versa. This is called lexical or static scoping. Understanding scope is crucial because it helps prevent naming conflicts and allows you to organize your code more effectively. It ensures that variables and functions are only accessed where they are intended to be used, avoiding unintended side effects and improving code clarity and maintainability.
&& and ||
The && (logical AND) and || (logical OR) operators are both used for Boolean logic in JavaScript, but they differ in their behavior: && (Logical AND): It returns true if both operands are truthy. If the first operand is falsy, it is immediately returned; otherwise, the second operand is evaluated and returned. In other words, it short-circuits and returns the first falsy value encountered, or the last truthy value if all operands are truthy. || (Logical OR): It returns true if at least one of the operands is truthy. If the first operand is
Var, let, const
The main difference between var, let, and const in JavaScript is their scope and mutability. var has function scope, allows redeclaration and reassignment, let has block scope, allows reassignment but not redeclaration within the same block, and const has block scope, does not allow reassignment, and the variable itself is read-only.
Return Statement
The return statement tells the interpreter that the invocation of the current function is complete. what three things happen?
this keyword
Used in constructor functions or within a class, to reference an instance of a created object. this.name assigns a passed in argument to the property "name" within this instance of an object.