Ternary operator example
let status = age >= 18 ? "adult" : "minor";
What is variable hoisting
var) in JavaScript are, in a sense, “hoisted” (or “lifted”) to the top of the function or statement.let and const are hoisted but not initialized. Referencing the variable in the block before the variable declaration results in a ReferenceError.Convert variable to Boolean
Two ways
Boolean(-1);!!value
Convert variable to Number
Number(" -12.34 ");
Convert variable to String
String(12.34);${x}toString() if supported on object
Number literal for hex, binary, BigNum numbers
0xff, 0b1101, 123n
Convert string to Float
parseFloat("12.5");
Convert string to Number
parseInt("101", 2); // binary to number
Conditionals: if
if (condition_1) {
statement_1;
} else if (condition_2) {
statement_2;
} else {
statement_last;
}Conditionals: switch
switch (expression) {
case label_1:
statements_1;
break;
default:
statements_def;
break;
}Conditionals: for loop (C like)
for (let i = 0; i < selectObject.options.length; i++) {
// index for - use by default
}
for (var i = 0, j = 9; i <= j; i++, j--) {
// Two variables at once
}Conditionals: While loop(s)
while (n < 3) {
n++;
x += n;
}
do {
i += 1;
console.log(i);
} while (i < 5);Conditionals: For each loop(s)
Creates a loop Iterating over iterable objects (including Array, Map, Set, arguments object and so on), invoking a custom iteration hook:
for (let i of arr) {
console.log(i);
}Will iterate all properties (numeric indexes + custom properties) or methods you define on Array object (or other object):
for (let i in obj) {
console.log(i);
}Exception handling structure
try {
throw new Error("The message");
} catch (e) {
throw e; // re-throw
} finally {
console.log("Resource cleanup...");
}JS Exceptions:
* How to throw
* Exception object properties
* How to create custom exception
throw new Error("The message");console.error(e.name); console.error(e.message); class CustomError extends Error {
constructor(message) {
super(message); // Pass message to the Error class
this.name = "CustomError"; // Set a custom error name
}
}
What happens when you return value in finally block?
It overwrites previous return (including one in catch block)
What is a closure. Use cases.
Closure is when a function remembers and continues to access variables from outside its scope, even when the function is executed in a different scope.
A closure is the combination of a function and the lexical environment within which that function was declared.
The Lexical Environment is a structure that holds identifier-variable mapping
Use cases:
* Data Encapsulation (Private Variables)
* Function Factories
* Memoization (Performance Optimization)
* Event Handlers (Retaining State)
* Iterators
* Partial Application / Currying
Variadic function parameters
function multiply(multiplier, ...theArgs) {
// theArgs is an actual array
return theArgs.map((x) => multiplier * x);
}Renamed export
export {function1 as newFunctionName};
Export from another module
export { name } from 'x.js'
Renamed import
import {function1 as newFunctionName} from "x"
JSON.parse(jsonString)JSON.stringify(obj)JSON.stringify(value, undefined, 2)
Second arg is replacer - a function that alters the behavior of the stringification process
How to handle cases where dog in adventurer.dog.name can be an undefined object? Name the techniques.
?? (nullish coalescing - scalania) and ?. (optional chaining)adventurer.dog?.name ?? "backup value"a.b?.c will return undefined insatead of Error?? "backup value" will return "backup value" only if the calculated value is undefined.Check typeof variable x
typeof x