How to Restrict a Number to a Certain Range in TypeScript ? Last Updated : 28 Apr, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report Restricting a number to a certain range in TypeScript means ensuring that a numerical value falls within specific bounds or limits. This process, often referred to as "clamping" or "bounding," prevents a number from going below a minimum value or exceeding a maximum value. For example, if you have a requirement that a variable representing a person's age should always be between 0 and 100, restricting the number to this range ensures that the age remains a valid value. Below are the approaches used to restrict a number to a certain range: Table of Content Using Math.min and Math.maxUsing Ternary OperatorApproach 1: Using Math.min and Math.maxIn this we use Math.min to ensure the value isn't below the minimum and Math.max to cap it at the maximum. Example: In this example, we are using Math.min and Math.max. JavaScript function numbers(value: number, min: number, max: number): { result: number, message: string } { const num = Math.min(Math.max(value, min), max); let message: string; if (num === value) { message = `Number ${value} is within the range of ${min} to ${max}.`; } else if (num === min) { message = `Number ${value} is below the lower limit of ${min}.`; } else { message = `Number ${value} exceeds the upper limit of ${max}.`; } return { result: num, message }; } let value: number = 15; let min: number = 5; let max: number = 10; let resultObject = numbers(value, min, max); console.log(resultObject.result); console.log(resultObject.message); Output: 10Number 15 exceeds the upper limit of 10.Approach 2: Using Ternary OperatorIn this we use ternary operator to check and limit the value within the specified range. Example: In this example, we are using Ternary Operator. JavaScript function numbers(value: number, min: number, max: number): { result: number, message: string } { const result = value < min ? min : value > max ? max : value; const message = result === value ? `Number ${value} is within the range of ${min} to ${max}.` : result === min ? `Number ${value} is below the lower limit of ${min}.` : `Number ${value} exceeds the upper limit of ${max}.`; return { result, message }; } let value: number = 15; let min: number = 5; let max: number = 10; let resultObject = numbers(value, min, max); // Output: 10 console.log(resultObject.result); // Output: "Number 15 exceeds the upper limit of 10." console.log(resultObject.message); Output: 10Number 15 exceeds the upper limit of 10. Comment More infoAdvertise with us Next Article How to enforce strict null checks in TypeScript ? A amanv09 Follow Improve Article Tags : TypeScript Similar Reads How to Convert String to Number in TypeScript? In TypeScript, converting a string to a number is a common operation that can be accomplished using several different methods. Each method offers unique advantages and can be chosen based on the specific requirements of your application. Below are the approaches to convert string to number in TypeSc 4 min read How to Iterate Over Characters of a String in TypeScript ? In Typescript, iterating over the characters of the string can be done using various approaches. We can use loops, split() method approach to iterate over the characters. Below are the possible approaches: Table of Content Using for LoopUsing forEach() methodUsing split() methodUsing for...of LoopUs 2 min read How to Return a TypeScript Interface that Changes based on Function Inputs ? In modern TypeScript development, creating adaptable interfaces that can dynamically adjust based on varying inputs is crucial for building flexible and maintainable codebases. TypeScript offers powerful features such as conditional types and mapped types, which enable developers to achieve this lev 2 min read How to Sort an Array in TypeScript ? Array sorting is the process of arranging the elements within an array in a specified order, often either ascending or descending based on predetermined criteria. Below are the approaches used to sort an array in typescript: Table of Content Method 1: Using sort methodMethod 2: Spread OperatorMethod 3 min read How to enforce strict null checks in TypeScript ? In Typescript to enforce strict null checks in tsconfig.json file, we need to enable "strictNullChecks" to true. When "strictNullChecks" is false, the language generally ignores variables of type null and undefined. If null and undefined is used in places where a definite value is expected, it raise 2 min read How to Specify that a Class Property is an Integer in TypeScript ? Specifying that a class property is an integer in TypeScript involves using type annotations or specific TypeScript features to define the data type of the property. Below are the approaches used to specify that a class property is an integer in TypeScript: Table of Content By using Type AnnotationB 2 min read Like