Coding best practices

See also:

System:

VARIABLES

JS – when you want a changeable or mutable variable use a let. For any data that should be protected from accidentally being overwritten, like an object, or an array or a function, use a const.

COMPARISON

JS – use === for absolute equality

FUNCTIONS

JS – use function expression – attached to a const – locally/block scoped – best practice

JS – anonymous functions using arrow functions – helps reduce memory usage

LOOPS

JS – use ‘foreach’ for arrays – use ‘for…in’ for objects

EVENTS

JS – Pass arguments through event listeners – BEST OF ALL WORLDS EVENT LISTENERS

LOADING

JS <script src=”JS/script.js” defer></script>

Use Modules to arrange code > NOTE – if you set the type to ‘Module’ they will automatically be deferred…

<script type=”module” src=”backpack.js”></script>

<script type=”module” src=”script.js”></script>

Caller-> import objectName from “./objectName.js”;

Callee-> export default objectName;

SCOPE – the Callee is scoped to the Caller, so not available from the browser console…

So therefore, it is better to use CLASSES (as templates) using this technique

* Class expression:  const Name = class {}

The ‘expression’ approach is currently the most favoured…

Resources