DEV Community

Omar for ClickPesa

Posted on • Originally published at ommyjay.me

Using JS Functions Properties in Real Life

In JavaScript, functions are first-class objects, because they can have properties and methods just like any other object. What distinguishes them from other objects is that functions can be called.

Consider the following logging function below...

function log(message, level) {
  console.log(message);
}

log('just logging...', 1) // "just logging..."
Enter fullscreen mode Exit fullscreen mode

You can use function property to avoid global variables for conditional checking

function log(message, level) {
  if (log.backup) {
    console.log(message);
  }
  console.log(message);
}

//set function property
log.backup = true;

log('backing it up...', 1) // "backing it up..."
Enter fullscreen mode Exit fullscreen mode

Top comments (0)