This document provides a collection of useful JavaScript functions for common programming tasks. These functions are designed to be simple, reusable, and beginner-friendly.
Generates a unique alphanumeric string with an optional specified length.
function unique_id(length = 32) {
let result = '';
let characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
// Start with a lowercase letter
result += characters.charAt(Math.floor(Math.random() * 26));
// Continue with full character set
for (let i = 1; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * characters.length));
}
return result;
}
- Purpose: Generate a unique identifier, useful for session tokens, element IDs, or other cases requiring uniqueness.
- Starts with Lowercase Letter: Ensures the first character is a lowercase letter, useful for HTML
id
attributes that should not start with a number.
console.log(unique_id()); // Example: "aP9X7kL4w0M6Z2j8n5Q3G1v9C6L7T5a"
console.log(unique_id(16)); // Example: "bX8Z6K4M9N1W3T2Y"
Returns an object containing details about the user's browser capabilities and environment.
function browserSpecs() {
let res = {};
res.navigator = Boolean(Navigator);
res.storage = Boolean(Storage);
res.worker = Boolean(Worker);
res.eventSource = Boolean(EventSource);
res.serviceWorker = Boolean(navigator.serviceWorker);
res.cookieEnabled = navigator.cookieEnabled;
res.platform = navigator.platform;
res.language = navigator.language;
return res;
}
- Purpose: Determine the browser's capabilities, such as support for web workers, storage, or service workers.
- Properties Returned:
navigator
,storage
,worker
,eventSource
,serviceWorker
: Boolean values indicating browser feature support.cookieEnabled
: Whether cookies are enabled.platform
: The user's operating system or device type.language
: The preferred language set in the browser.
console.log(browserSpecs());
/*
Example Output:
{
navigator: true,
storage: true,
worker: true,
eventSource: true,
serviceWorker: true,
cookieEnabled: true,
platform: "Win32",
language: "en-US"
}
*/
Converts numbers between different bases (e.g., decimal to hexadecimal, hexadecimal to decimal).
function baseConverter(num, base, newBase) {
const substitute = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
if (newBase > base) {
let remainders = [];
let last = false;
while (Math.floor(num / newBase) !== 0 || last !== true) {
if (Math.floor(num / newBase) === 0) {
last = true;
}
remainders.push(substitute[num % newBase]);
num = Math.floor(num / newBase);
}
return remainders.reverse().join('');
} else {
let letters = num.split('').reverse();
let numbers = [];
letters.forEach((item, i) => {
numbers.push(substitute.indexOf(item.toUpperCase()) * Math.pow(base, i));
});
return numbers.reduce((a, b) => a + b, 0);
}
}
- Purpose: Convert numbers between different bases, such as binary, decimal, hexadecimal, etc.
- Supports Two Cases:
- Base Expansion: Converts numbers to a higher base (e.g., decimal to hexadecimal).
- Base Reduction: Converts numbers from a higher base to a lower base (e.g., hexadecimal to decimal).
- Substitute Array: Maps digits to characters for bases above 10 (e.g.,
A
for 10,B
for 11).
// Convert 1250 (decimal) to hexadecimal
console.log(baseConverter(1250, 10, 16)); // Output: "4E2"
// Convert "4E2" (hexadecimal) to decimal
console.log(baseConverter('4E2', 16, 10)); // Output: 1250
- Assumes input validity; invalid characters or bases are not handled.
- Supports bases up to 36 due to the English alphabet limit.
-
Error Handling:
- Add input validation to ensure valid numbers and bases.
- Handle edge cases like zero or negative numbers in the base converter.
-
Browser Capability Checks:
- Expand
browserSpecs
to include newer features like WebAssembly or WebRTC.
- Expand
-
ID Uniqueness:
- For critical use cases (e.g., session tokens), consider using a UUID library like
uuid
for better guarantees.
- For critical use cases (e.g., session tokens), consider using a UUID library like
-
Optimizing Base Conversion:
- Use built-in JavaScript functions like
parseInt
andtoString
for simple conversions:console.log(parseInt('4E2', 16)); // 1250 console.log((1250).toString(16).toUpperCase()); // "4E2"
- Use built-in JavaScript functions like
These utility functions provide practical solutions for common JavaScript tasks:
- Generating unique IDs for dynamic elements or identifiers.
- Retrieving browser specifications for feature detection.
- Converting numbers between bases for data manipulation or formatting.