JavaScript does not have a built-in function like PHP’s `range`, but you can easily implement a function to achieve similar functionality. Here’s how you could create a function that generates a range of numbers or characters:
For Numbers
function range(start, end) { const arr = []; for (let i = start; i <= end; i++) { arr.push(i); } return arr; } console.log(range(1, 3)); // [1, 2, 3]
For Characters
function charRange(start, end) { const arr = []; for (let i = start.charCodeAt(0); i <= end.charCodeAt(0); i++) { arr.push(String.fromCharCode(i)); } return arr; } console.log(charRange("A", "C")); // ["A", "B", "C"]
Combined Function
function rangeGeneric(start, end) { const arr = []; if (typeof start === 'number' && typeof end === 'number') { for (let i = start; i <= end; i++) { arr.push(i); } } else if (typeof start === 'string' && typeof end === 'string') { for (let i = start.charCodeAt(0); i <= end.charCodeAt(0); i++) { arr.push(String.fromCharCode(i)); } } return arr; } console.log(rangeGeneric(1, 3)); // [1, 2, 3] console.log(rangeGeneric("A", "C")); // ["A", "B", "C"]