Skip to content
DeveloperMemos

Checking if a Value is Within a Range of Numbers using TypeScript

TypeScript, Range, Numbers2 min read

When working with numerical data, it is often necessary to determine whether a value falls within a specific range. This can be useful in a variety of scenarios, such as validating user input, filtering data, or implementing conditional logic based on a range of values. In this article, we will explore how to check if a value is within a range of numbers using TypeScript.

Approach 1: Using Logical Operators

One straightforward approach to check if a value is within a range is by using logical operators. TypeScript provides logical operators such as greater than (>), less than (<), greater than or equal to (>=), and less than or equal to (<=) that can be used for this purpose.

Here's an example that demonstrates how to check if a value is within a range using logical operators:

1const value = 42;
2const minValue = 10;
3const maxValue = 100;
4
5if (value >= minValue && value <= maxValue) {
6 console.log("The value is within the range.");
7} else {
8 console.log("The value is outside the range.");
9}

In this example, we have a value of 42 and we want to check if it falls within the range defined by minValue and maxValue. The logical expression value >= minValue && value <= maxValue evaluates to true if the value is within the range, and false otherwise.

Approach 2: Using Range Checking Function

Another approach is to encapsulate the range checking logic in a function. This can make the code more reusable and easier to understand, especially if you need to perform range checks in multiple places.

Here's an example of a range checking function using TypeScript:

1function isValueInRange(value: number, minValue: number, maxValue: number): boolean {
2 return value >= minValue && value <= maxValue;
3}
4
5const value = 42;
6const minValue = 10;
7const maxValue = 100;
8
9if (isValueInRange(value, minValue, maxValue)) {
10 console.log("The value is within the range.");
11} else {
12 console.log("The value is outside the range.");
13}

In this example, we define a isValueInRange function that takes three parameters: value, minValue, and maxValue. The function returns true if the value is within the range, and false otherwise. We then call this function with the appropriate arguments to perform the range check.

Approach 3: Using TypeScript Type Guard

If you are working with custom types or want to enforce type safety, you can use TypeScript type guards to perform range checks. A type guard is a runtime check that determines the type of a value at runtime and narrows its type within a specific code block.

Here's an example of using a TypeScript type guard to check if a value is within a range:

1function isValueInRange(value: number, minValue: number, maxValue: number): value is number {
2 return value >= minValue && value <= maxValue;
3}
4
5const value: unknown = 42;
6const minValue = 10;
7const maxValue = 100;
8
9if (isValueInRange(value as number, minValue, maxValue)) {
10 console.log("The value is within the range.");
11} else {
12 console.log("The value is outside the range.");
13}

In this example, we define the isValueInRange function as a type guard by specifying value is number as the return type. The function performs the range check and returns true if the value is within the range. We then use the type assertion value as number to narrow down the type of the value variable before passing it to the type guard.

Conclusion

Checking if a value is within a range of numbers is a common task when working with numerical data. In this article, we explored different approaches to accomplish this using TypeScript. We discussed using logical operators, encapsulating the range check in a function, and utilizing TypeScript type guards for type safety. Depending on your specific requirements and coding style, you can choose the approach that best suits your needs.