How JS Programs Work & the Execution Context
var declaration
*Least strict in its use. *The same variable can be redeclared using the keyword word many times throughout the running of a program. * Declares a variable in global memory, function scope or block scope.
Code Execution Phase
The program is put on the call stack and each statement is processed one at a time.
function
A block of reusable code that is called by name. It can be passed parameters/arguments needed to operate and can optionally return data (the return value); not associated with an object.
Child Function
A function created inside another function block.
Implicit function return
A function returns back to the calling program after all function code is processed. No return statement is written.
Explicit function return
A function returns back to the calling program when a return statement processed.
Asynchronous programming
A means of parallel programming in which a unit of work runs separately from the main application thread and notifies the calling thread when it is completed.
Interpreter
A program that processes code line by line to convert it into machine language and does not create an executable file.
Multi-threaded process
A single process where each thread has its own program counter, stack and set of registers, but sharing common code, data, and certain structures such as open files and input.
Undefined
A special value created for JavaScript that is used to give hoisted variables a value during memory allocation during the memory creation phase.
The error: uncaught syntax error: Identified 'a' has already been declared occurs when:
A variable declared using let can not be re-declared in the same scope.
Variable Environment
Another name for the Memory Creation phase.
Variables declared inside a random block {}, go into this memory space:
Block memory space
JavaScript has 3 types of scope:
Block scope Function scope Global scope
Synchronous programming
Code is executed in sequence where each statement must wait for the previous statement to finish before executing the next.
Function Invocation/ Call
Code that calls a function into action
Function Execution Context(FEC)
Created for each function call and each has there own memory creation(allocation) code execution phases and stays in the call stack until the function ends. 1 per function.
Symbols that identify a JavaScript code block
Curly Brackets { }
Variable Shadowing
If two variables have the same name, and exist inside of the same scope or scope chain where the variable with the more specific scope takes precedence.
Runtime Reference Error
Is thrown when a code attempts to reference a non-existing variable or function.
Garbage Collector
In a managed memory system, the program that periodically runs to free unreferenced memory or pop-off unused statements in the call stack.
Thread
In computer science, the execution is the smallest sequence of programmed instructions that can be managed independently by a scheduler, which is typically a part of the operating system and is a component of a process.
Hoisting
Is JavaScript's default behavior of allocating variables in memory during the memory creation phase before the code executes in the code execution phase allowing the program to access these variable before they are initialized.
Closures
Is a function within another function with access to the outer function variables. The definition itself seems pretty straightforward, but the scope makes this definition unique. Inner functions (closure) can access variables defined in their scope (variables defined between curly brackets), their parent functions, and inside global variables. Now, here you need to keep in mind that the outer function cannot have access to the inner function variables.
use strict
Is a way to voluntarily enforce stricter parsing and error handling on your JavaScript code at runtime. Code errors that would otherwise have been ignored or would have failed silently will now generate errors or throw exceptions. In general, it is a good practice.
JS closure
Is the combination of a function bundled together (enclosed) with references to its surrounding state and which gives you access to an outer (parent) function's scope from an inner (child) function.
Memory Creation Phase
Memory is allocated for each variable and function declared. All variables allocated in this phase as set to a special value called undefined. For functions, the entire function code block is loaded into memory.
let declaration
Moderately strict in its use where allows you to declare variables that are limited to the scope of a block statement, or expression on which it is used. Never declared in the Global Memory Scope and can not be redeclared in the same program.
const declaration
Most strict in its use, it must be initialized at the moment it is declared. The same variable can not be redeclared in the program and never declared in Global Memory Scope
Call Stack
The internal structure that keeps track of the sequence of statements that have been called.
Lexical Environment
Parses code to allocate memory for variables and functions and how scope is defined for each.
Syntax
Rules of the programming language
Scope Chain
Scopes can be nested where inside an inner scope you can access the variables of an outer scope.
Variables declared using the keyword let go into this memory space from the time it is declared to the time it is initialized:
TDZ
Global Execution Context(GEC)
The default execution context in which the main JS program starts its execution. 1 per program and stays available in the call stack until the program ends.
Execution Context
The environment in which the JavaScript code is executed that has 2 phases - the memory creation phase (variable environment) and the code execution phase(call stack).
Scope
The space in memory where an item, such as a variable or a function, is visible and accessible in your code.
Single-threaded process
There is one program counter, and one sequence of instructions that can be carried out at any given time.
Keywords
These are words that are reserved by a programming language and are used for a particular purpose and may not be used as variable or function names.
Script Scope
This is a special place where variables declared in the main program using the keywords "let" or "const" can be accessed instead of being placed in the Global memory scope
Not Defined
This is an error that occurs when a program tries to reference a variable of function that has either not been declared at all or has been declared outside of the accessible scope chain.
Thread of Execution
This is another name for the Code Execution Phase.
Temporal Dead Zone(TDZ)
This is the time from when a variable is declared in memory using the keyword "let" to the time it is initialized. If you try to use the variable before it is initialized, you will get a reference error.
key:value pair
Variables allocation in memory and assigned undefined during the memory creation phase. Then assigned a value when initialized during the code execution phase.
Variable Initialization
Variables are set equal to a value other than undefined during the code execution phase.
Function (memory) Scope
Variables declared inside a function have function scope and are visible only to code that appears inside that function including child functions also is referred to as having local scope.
The error: uncaught syntax error: Missing initializer in const declaration occurs when this does not occur:
Variables declared using const must be initialized on the same line it is declared.
Block (memory) Scope
Variables declared using let or const in a block {}, have this scope and their values can be reference ONLY with in the {} block.
Global (memory) Scope
Variables that can be accessed from anywhere in a JavaScript program.
The error: uncaught type error: Assigned to constant variable occurs when:
When a variable is declared using const and it is redeclared in the same scope.
Illegal Variable Shadowing
While shadowing a variable, it should not cross the boundary of the scope, i.e. we can shadow var variable by let variable but cannot do the opposite.
Variables declared inside a function, go into this memory space:
function memory space