Skip to content

Instantly share code, notes, and snippets.

[JavaScript] - Passing Arguments to Event Handlers

SOURCE

<nav class="nav">
        <img
          src="img/logo.png"
          alt="Bankist logo"
          class="nav__logo"
          id="logo"

[JavaScript] - Array Method Summary

SOURCE

To Mutate Original Array

  • Add to original:
    • .push (end)
    • .unshift (start)
  • Remove from original:
    • .pop (end)
  • .shift (start)

[JavaScript] - Array Methods

SOURCE

Simple Methods

let arr = ['a', 'b', 'c', 'd', 'e'];

// SLICE
console.log(arr.slice(2)); // ['c', 'd', 'e'], create a new aray starting at index index 2 and doesn't mutate the original array
console.log(arr.slice(2, 4)); // ['c', 'd'], start at index 2 and end at 4, 4 is exclusive

[JavaScript] - Immediately Invoked Function Expressions (IIFE)

SOURCE

const runOnce = function () {
  console.log('This will never run again');
};
runOnce();

// IIFE

[JavaScript] - Function Accepting Callback Function

SOURCE

const oneWord = function (str) {
  return str.replace(/ /g, '').toLowerCase();
};

const upperFirstWord = function (str) {
 const [first, ...others] = str.split(' ');

[JavaScript] - First Class vs Higher Order Functions

SOURCE

First Class Functions

  • JS treats function as first class citizens.

  • This means that functions are simply values.

  • Functions are just another "type" of object.

    • Store functions in variables or properties:

[JavaScript] - How Passing Arguments Works: Value vs Reference

SOURCE, SOURCE

JavaScript does not have pass by reference. The object passing might looks like passing by reference but it is still passing by value. The value here is the memory address in the stack that points to the object in the heap.

Again, JavaScript is a pure pass by value language. It's just that the values being passed around are references when not dealing with primitives

const flight = 'LH234';
const jonas = {

[JavaScript] - Arrays vs Sets and Objects vs Maps

Arrays:

task = ['Code', 'Eat', 'Code'];
  • Use when you need ordered list of values (might contain duplicates).
  • Use when you need to manipulate data

Sets:

[JavaScript] - Number Casting

SOURCE

Why JS have Number(expression) and new Number(expression)?

Answer:

Boolean(expression) will simply convert the expression into a boolean primitive value, while new Boolean(expression) will create a wrapper object around the converted boolean value.

The difference can be seen with this: