JS Fundamentals Flashcards
What is the difference between undefined, null and undeclared?
Undefined is a value that has been declared but not defined.
Null is an intentional value to convey the absence of a value.
Undeclared is value that has not been declared (using var, let, const, etc.).
What is a primitive value?
Data that is not an object and has no methods and are immutable.
What are the six primitive values?
- ) String
- ) Number
- ) BigInt
- ) Boolean
- ) Undefined
- ) Symbol
Of what type are variables that are NOT primitive?
Object
What are the two basic groups of data types in JavaScript?
Primitive and Reference Types
What is hoisting?
JavaScript’s default behavior of moving variable declarations, function declarations and class declarations to the top.
What’s the difference between declarations and initializations?
Declaration: declaring a variable (ex. var text;)
Initialization: initializing a value for a variable (text = “123”)
What gets hoisted to the top?
Declarations NOT initializations!! Function declarations and NOT function expressions!
What value is a variable assigned when declared with “var”?
undefined
What value is a variable assigned when declared with “const” or “let”?
It will not be initialized. It will result in an error
Are JavaScript classes hoisted?
Yes, only class declarations. They result in same as variables declared with “const” or “let” where they are uninitialized until evaluation. Safer to write a class and then use it.
Class expressions are NOT hoisted!
Can an arrow function be used as a constructor function?
No. Arrow functions do not have its own “new.target” property so it cannot be used as a constructor function.
It cannot be called by “new” as there is no internal method that allows it.
They also don’t have the “prototype” property.
What is “new.target”?
A property that lets you detect whether a function or constructor was called using the “new” operator.
In constructors and functions invoked using the new operator, “new.target” returns a reference to the constructor or function.
In normal function calls, “new.target” is undefined.
What are the types of scope available in JavaScript?
There are two types:
- ) Local Scope
- ) Global Scope
What type of scope does JavaScript have?
Function scope, where each function creates a new scope.
What are local variables in JavaScript?
Variables that are only visible in the function where it was defined.
They are created when a function starts and deleted when the function is completed.
What are global variables in JavaScript?
Variable declared outside a function. Can be accessed by any function.
What happens when you assign a value to a variable that hasn’t been declared?
It becomes global.
What happens when you assign a value to a variable that hasn’t been declared, but you’re running in “strict mode”?
The variable does NOT become global.
What is the call method?
A method that allows you to use one method on different objects.
What is the apply method?
Like the “call” method, a method that allows you to use one method on different objects.
What’s the difference between the .call and .apply methods?
They are the same except that the “call” method accepts each parameter to be passed individually whereas the “apply” method accepts an array of parameters as the second parameter. The first parameter is the context of “this”.
How do you call the “call” method?
{method you want to be shared}.call({context of “this”}, {parameters, delimited by a comma})
How do you call the “apply” method?
{method you want to be shared}.apply({context of “this”}, [{array of parameters}])
What happens when you don’t pass all the arguments needed for a method where you use “call” or “apply”?
The rest of the parameters will be undefined.
What does the “bind” method do?
It creates a new function of the function that it’s being called on and attaches the “this” context to it so that it can be called at a later time.
What is a closure?
A function that can access the variables of an outer scope.
It’s important to note that an outer function will not have access to an inner scopes’ variables.
Why would we use a closure?
- ) Data Privacy
- ) Currying
- ) Event Handlers
- ) Async Callback Functions
Is JavaScript a single or multi-threaded language?
Single
What is a call stack?
A stack that receives line by line of code by the interpreter. As they execute, they pop from the stack (from the back).
What is the event table?
When the interpreter, while in the process of placing code lines into the call stack, comes across asynchronous code, it places it in the event table.
A table that holds asynchronous code and moves it to an event queue once the allotted time necessary for the code to run happens.
What is the event queue?
A queue that receives asynchronous code from the event table and is responsible for passing it to the event loop.
What is the event loop?
Process that continuously checks on the call stack and the event queue.
Makes sure to take code from the event queue (from the front) once the call stack is empty.
What is one benefit to using “let” vs “var” in async code and closures?
“let” creates a new binding for variables, while “var” keeps the same binding.