ClickPesa Tech

Follow

ClickPesa Tech

Follow

Using JS Functions Properties in Real Life

Omar's photo
Omar
·Apr 26, 2022·

1 min read

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..."

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..."
 
Share this