JS Reserved Words Flashcards
What is ‘this’?
refers to an object:
In an object method, this refers to the object.
In an object’s prototype chain, this refers to the object.
In a function, in strict mode, this is undefined.
In an event, this refers to the element that received the event.
Alone, this refers to the global object.
In a function, this refers to the global object.
methods like call(), apply(), and bind() can refer this to any object.
What are the JavaScript primitive data types?
7 primitives:
string, number, boolean, null, undefined, symbol, bigint
break;
the break statement ends the current loop, switch, or labeled block.
continue;
the continue statement ends the current iteration of a loop.
i.e. if we are on the 9th iteration and hit a continue statement, we’ll skip to the 10th iteration.
return;
the return statement ends function/method execution and specifies a value to be returned to the caller.
switch case
the two keywords needed to assemble a switch statement.
switch(input) { case 'a': //run some code break; default: alert('no code ran'); break; }
if (true)
else…
the if keyword defines a conditional expression
the else keyword defines a block of code if not true
for ()
creates a loop that will iterate ‘for’ a defined amount of iterations
while (true)
creates a loop that will iterate for an undefined amount of iterations.
do { //some code } while (true);
a do while expression will execute code exactly once before considering the while expression
true
false
reserved keywords for boolean values
typeof foobar
returns a string indicating the type of foobar
[ undefined, object, boolean, number, bigint, string, symbol, function, ]
note: null is a type of object
foo instanceof bar
the instance of operator tests if the prototype property of a constructor appears in the prototype chain of an object.
returns true||false
delete
the delete operator removes a property from an object.
delete object.property;
in
the in operator returns true if the specified property exists in the object’s prototype chain
‘propertyName’ in object;
if (‘propertyName’ in object === false) //do this code;
const
const foo = ‘bar’
Constants are block-scoped. The value of a constant can’t be changed through reassignment (i.e. by using the assignment operator), and it can’t be redeclared.
However, if a constant is an object or array its properties or items can be updated or removed.
let
The let declaration declares a block-scoped local variable, optionally initializing it to a value.
var
The var statement declares a function-scoped or globally-scoped variable, optionally initializing it to a value.
Which is preferrable to use ‘let’ or ‘var’
The vast majority of the time, ‘let’ is preferable to use for variable containers.
import
The static import declaration is used to import read-only live bindings which are exported by another module.
The imported bindings are called ‘live bindings’ as they are updated by the module that exported the binding, and cannot be modified by the importing module.
In order to use the import declaration in a source file, the file must be interpreted by the runtime as a module. In HTML, this is done by adding type=”module” to the tag.
Modules are automatically interpreted in strict mode.
import declarations can only be present in modules, and only at the top-level (i.e. not inside blocks, functions, etc.).
What are the four forms of import declarations?
Named import: import { export1, export2 } from "module-name"; Default import: import defaultExport from "module-name"; Namespace import: import * as name from "module-name"; Side effect import: import "module-name";
export
The export declaration is used to export values from a JavaScript module.
export default
The export default syntax allows any expression.
As a special case, functions and classes are exported as declarations, not expressions, and these declarations can be anonymous.
This means functions will be hoisted.
async
async function (parameter) { const result = await expression; return result; }
An async function is a function declared with the async keyword, and the await keyword is permitted within it.
The async and await keywords enable asynchronous, promise-based behavior to be written in a cleaner style, avoiding the need to explicitly configure promise chains.
await
The await expression causes async function execution to pause until a Promise is settled (that is, fulfilled or rejected).
AND to resume execution of the async function after fulfillment.
When resumed, the value of the await expression is that of the fulfilled Promise.
If the Promise is rejected, the await expression throws the rejected value.
It can only be used inside an async function within regular JavaScript code. However, it can be used on its own with JavaScript modules.
null
The value null represents the intentional absence of any object value. It is one of JavaScript’s primitive values and is treated as falsy for boolean operations.
class Name {}
The class declaration creates a new class with a given name using prototype-based inheritance.
class name { constructor() }
static
The static keyword defines a static method or property for a class, or a class static initialization block.
Neither static methods nor static properties can be called on instances of the class. Instead, they’re called on the class itself.
class Name { static value; constructor() static function(){} }
extends
The extends keyword is used in class declarations or class expressions to create a class that is a child of another class.
constructor()
The constructor method is a special method of a class for creating and initializing an object instance of that class.
super
The super keyword is used to access properties on an object literal or class’s [[Prototype]], or invoke a superclass’s constructor.
try, catch, finally
The try…catch statement is comprised of a try block and either a catch block, a finally block, or both.
The code in the try block is executed first, and if it throws an exception, the code in the catch block will be executed.
The code in the finally block will always be executed before control flow exits the entire construct.
throw
The throw statement throws a user-defined exception. Execution of the current function will stop
(the statements after throw won’t be executed),
and control will be passed to the first catch block in the call stack. If no catch block exists among caller functions, the program will terminate.
for await…of
The for await…of statement creates a loop iterating over async iterable objects as well as on sync iterables,
including: built-in String, Array, Array-like objects (e.g., arguments or NodeList), TypedArray, Map, Set, and user-defined async/sync iterables.
This statement can only be used inside an async function.
async function* foo() {
yield 1;
yield 2;
}
(async function() { for await (const num of foo()) { console.log(num); break; // closes iterator, triggers return } })();
for. .. in
for. .. of
***less performant that .forEach() and standard for()
The for…in statement iterates over all enumerable properties of an object that are keyed by strings (ignoring ones keyed by Symbols), including inherited enumerable properties.
The for…of statement creates a loop iterating over iterable objects, including: built-in String, Array, array-like objects (e.g., arguments or NodeList), TypedArray, Map, Set, and user-defined iterables. It invokes a custom iteration hook with statements to be executed for the value of each distinct property of the object.