Declaring a function with let?


#1

I was wondering if there’s ever a context where a function is declared using let instead of const. My first guess is that’s not possible/necessary but I’m curious.


#2

The idea of let is that its value can be altered, and even its type. Consider the following frivolous example…

const multifunction = () => {
  let x = 50;
  do {
    let func = Math.random() < 0.5 ? n => n / 10 : n => n * 10;
    x = func (x);
    console.log(x);
  } while (x > 1);
}

multifunction();

Because our function is declared with let we can swap out its body at will. Above the function func alternates randomly between two functions.

Try it… https://repl.it/@mtf/multifunction-with-let


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.