JavaScript - Accept only numbers between 0 to 255 range?

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

To accept number in a range and check, use the if else condition.

Example

Following is the code −

function checkRangeDemo(number) {
   if (number < 0 || number > 255) {
      return false;
   }
   else {
      return true;
   }
}
var number = 55;
if (checkRangeDemo(number) == true)
   console.log(number + " is in range")
else
   console.log(number + " is not in range")
var number1 = 350;
if (checkRangeDemo(number1) == true)
   console.log(number1 + " is in range")
else
   console.log(number1 + " is not in range")

To run the above program, you need to use the following command −

node fileName.js.

Here, my file name is demo261.js.

Output

This will produce the following output on console −

PS C:\Users\Amit\javascript-code> node demo261.js
55 is in range
350 is not in range
raja
Published on 09-Nov-2020 07:35:42
Advertisements