Languages
[Edit]
EN

JavaScript - flags usage example (multiple bit flags in one variable)

0 points
Created by:
Lilly-Grace-Greig
211

In this article, we would like to show you how to use flags in JavaScript.

1. Practical example

In this example, we use left shift operator (<<) to create the flags by shifting 1 various number of bits so each flag is different. Then we use the flags to enter the specific mode.

// ONLINE-RUNNER:browser;

let counter = 0;

const MyFlags = {
  DIRECTORY: 1 << counter++,
  FILE: 1 << counter++,
  SHORTCUT: 1 << counter++
};


// Usage example:

console.log('Available flags: ', JSON.stringify(MyFlags, null, 4));
console.log('Used flags:');

var flags = MyFlags.DIRECTORY | MyFlags.FILE;

if (flags & MyFlags.DIRECTORY) {
  console.log('Directory mode used.');
}
if (flags & MyFlags.FILE) {
  console.log('File mode used.');
}
if (flags & MyFlags.SHORTCUT) {
  console.log('Shortcut mode used.');
}

Output:

Available flags: , {
    "DIRECTORY": 1,
    "FILE": 2,
    "SHORTCUT": 4
}
Used flags:
Directory mode used.
File mode used.

2. Safer solution

In this example, we create mechanism that protects the counter global variable from being accidentally overwritten.

// ONLINE-RUNNER:browser;

const MyFlags = new function () {
  var counter = 0;
  return {
    DIRECTORY: 1 << counter++,
    FILE: 1 << counter++,
    SHORTCUT: 1 << counter++
  };
};


// Usage example:

console.log('Available flags: ', JSON.stringify(MyFlags, null, 4));
console.log('Used flags:');

var flags = MyFlags.DIRECTORY | MyFlags.FILE;

if (flags & MyFlags.DIRECTORY) {
  console.log('Directory mode used.');
}
if (flags & MyFlags.FILE) {
  console.log('File mode used.');
}
if (flags & MyFlags.SHORTCUT) {
  console.log('Shortcut mode used.');
}

Output:

Available flags: , {
    "DIRECTORY": 1,
    "FILE": 2,
    "SHORTCUT": 4
}
Used flags:
Directory mode used.
File mode used.

Note:

In this solution, we used the counter global variable protection mechanism from the following file:

3. Reusable function with arguments

In this example, permissions flags are passed to the function where we check the access to the specified resource.

// ONLINE-RUNNER:browser;

let counter = 0;

const MyFlags = {
	ADMIN: 1 << counter++,
	MODERATOR: 1 << counter++,
	USER: 1 << counter++
};


// Usage example:

const grantPermissions = (resource, permissions) => {
	console.log('Granted permissions:');
	if (permissions & MyFlags.ADMIN) {
		console.log(' + Admin permissions granted for ' + resource + '.');
	}
	if (permissions & MyFlags.MODERATOR) {
		console.log(' + Moderator permissions granted for ' + resource + '.');
	}
	if (permissions & MyFlags.USER) {
		console.log(' + User permissions granted for ' + resource + '.');
	}
};

grantPermissions('backups', MyFlags.ADMIN | MyFlags.MODERATOR);
grantPermissions('review', MyFlags.MODERATOR);
grantPermissions('reading', MyFlags.USER);

Output:

Granted permissions:
 + Admin permissions granted for backups.
 + Moderator permissions granted for backups.
Granted permissions:
 + Moderator permissions granted for review.
Granted permissions:
 + User permissions granted for reading.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join