IES: JS-deck 12 Flashcards
1
Q
JS Asterisk
A
- the arithmetical multiplication operator in JavaScript
2
Q
JS Function Block
A
- can include a Return statement
- Common for statements in a function block to include calls to functions outside that function block
3
Q
JS Return Statement
A
- Can be included in / omitted from a JS function block
- without a value specified, function will return “undefined”
- Causes script flow to continue at caller
- No further function statements get executed
- Typical to pass manipulated argument values result to caller
Ex.
function function-name ( parameter, parameter ) {
// Statements to be executed go here.
return result
}
4
Q
JS Invoke
A
- definition: called to execute their statements whenever required
- A usefulness of JS “functions”
- Entering different arguments, a caller can effect different results
5
Q
JS Parentheses
A
- JS operator
- Component of the call statement
- What actually calls the function
- A statement can assign a function to a variable by specifying just the function name (i.e. without the ( ) operator).
- A function name followed by ( ) will invoke the function and will return the assigned function value
6
Q
JS Interpreter
A
- reads scripts in top-to-bottom order
- Makes two sweeps
1. First Sweep: - Looks for function declarations
- remembers any found in a process known as “hoisting”
2. Second sweep: - the script is executed
7
Q
JS Hoisting
A
- the process whereby the interpreter appears to move the declaration of functions, variables, classes, or imports to the top of their scope, prior to execution of the code
- Occurs in the first sweep of the JS interpreter
- The JS interpreter looks for and remembers function declarations
- Allows function calls to appear in the script before the function declaration
- (the first sweep does not recognize functions using “let” or “const” keywords to assign variables)
8
Q
JS self-invoking function expressions
A
- a.k.a. IFFE (often pronounced- “iffy”)
- IFFE: Immediately Invoked Function Expressions
- Anonymous function expressions can be made “self-invoking” by enclosing entire function in ( ) and adding the ( ) operator at the end of the expression
syntax:
(function ( ) {statements; return value}) ( ) - This means that their statements are automatically executed one time when the script is first loaded by the browser
- They execute their statements one time ONLY.
9
Q
JS anonymous functions
A
- a function name can be omitted when assigning a function to a variable
- This omission is possible because the function can be called in a statement specifying the variable name and the ( ) operator
- When assigning a named function to a variable, only specify the function name in the statement
syntax:
let variable = function (parameters) {statements; return value} - Can be made “self-invoking” by enclosing entire function in ( ) and adding the ( ) operator at the end of the expression
syntax:
(function ( ) {statements; return value} ) ( )
10
Q
JS “lexical scope”
A
- the environment in which the variable was created
- Can be either “Global” or “local”
- Determines the extent to which variables are accessible in their scripts
11
Q
JS “global scope”
A
- A lexical scope
- refers to variables created outside function blocks
- Accessible globally during entire script (exists continuously and is available to functions within same script environment)
- can cause conflicts: can allow for variables of the same name but with different values to exist in the same environment
- Conflict potential is best avoided by never creating global variables which store primitive values
12
Q
JS “primitive values”
A
- all data types except “object” and “function”
13
Q
JS “local scope”
A
- a lexical scope
- variables created inside function blocks (Good practice: declare variables at the very beginning of a function block)
- Accessible locally throughout the function life (exists until function ends or until it returns)
- Recommended: try to create only local variables to store values within your scripts (avoids conflicts: like-named variables can exist within separate, included functions in the same script)
14
Q
JS store-value keywords
A
- “var”: allows like-named conflicting variables to overwrite their assigned values without warning
1. An older keyword - “let” and “const”:
1. More recent keywords
2. Prohibit overwriting their assigned values without warning
3. recognize like-named conflicting variables as an “Uncaught SyntaxError” - Recommended: create variables declared using the “let” or “const” keywords to store values within your scripts
15
Q
JS Closures
A
- A function nested inside an outer function that retains access to variables declared in the outer function
1. because that is the lexical scope in which the nested function was created - Prevents using global variables to store primitive values
- Used with occasional need or desire for global variables
Ex.
Remembering score count as script proceeds
16
Q
JS Prototype
A
- all JS objects inherit properties and methods from a prototype
- Standard JS objects, such as functions, call an internal constructor function to create the object by defining its components
17
Q
JS Closures (b)
A
- Can use console.log(add) to confirm that the function expression has been assigned to the outer variable
- “Constructor” Dropdown:
1. The assigned function has a special (Closure) Scope in addition to…
2. ..the regular local (Script) scope and…
3. …outer (Global) scope. - How the count variable remains accessible via the assigned function yet, importantly, cannot be referenced in any other way
- How closures hide persistent variables from other parts of your script is an important concept
Ex.
Similar to how “private” variables are hidden in other programming languages and are only accessible via “getter” methods.
18
Q
JS Primitive Data Types:
A
- The basic data types that represent a single value
1. String: Represents textual data, enclosed in single quotes, double quotes, or backticks.
Ex.let str1 = 'Hello'; let str2 = "World";
2. Number: Represents numeric values, including integers and floating-point numbers.
Ex.let num1 = 10; let num2 = 3.14;
3. BigInt: Represents integers of arbitrary precision, allowing for storage and operations on large integers beyond the safe integer limit of the Number type.
Ex.let bigInt = 1234567890123456789012345n;
4. Boolean: Represents logical values, either true or false.
Ex. ```
let bool1 = true;
let bool2 = false;
~~~
5. Undefined: Represents a variable that has been declared but has not been assigned a value.
Ex.let undef;
6. Null: Represents the intentional absence of a value.
Ex.let n = null;
7. Symbol: Represents a unique and immutable value, often used as object property keys.
Ex.const sym1 = Symbol('description');
19
Q
JS Non-primitive Data Types
A
- Data types that can hold collections of values or more complex entities.
1. Object: Represents a collection of key-value pairs, where keys are strings (or symbols) and values can be any data type
Ex.let person = { name: 'John', age: 30, city: 'New York' };
2. Array: Represents an ordered list of values, which can be of any data type.
Ex.let numbers = [1, 2, 3, 4, 5]; let mixedArray = [1, 'hello', true, null];
20
Q
JS “typeof”
A
- Operator
- Can be used to determine the data type of a variable or value.
Ex.console.log(typeof 'hello');
// Output: “string”console.log(typeof 123);
// Output: “number”console.log(typeof true);
// Output: “boolean”console.log(typeof undefined);
// Output: “undefined”console.log(typeof null);
// Output: “object” (historical quirk, should be “null”)console.log(typeof {});
// Output: “object”console.log(typeof []);
// Output: “object”
21
Q
JS parseInt( )
A
- can return an integer whole number version, in the number data type, of a string specified within its parantheses
Ex.parseInt('42')
return = 42parseInt('42nd Street')
return = 42 - built-in function
- returns “NaN” if a numeric value is not found at the beginning of the specified string
22
Q
JS parseFloat()
A
- Can return a floating-point number version, in the number data type, of a string specified within its parantheses
Ex.parseInt('3.14')
return = 3.14parseInt('3.14th Street')
return = 3.14 - built-in function
- returns “NaN” if a numeric value is not found at the beginning of the specified string