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.