Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Number.isInteger(x) which is created can not work in IE

Number.prototype.isInteger = Number.prototype.isInteger || function(x) {
  return (x ^ 0) === x;
}
console.log(Number.isInteger(1));

will throw error in IE10 browser

like image 232
huangxbd1990 Avatar asked Oct 21 '14 08:10

huangxbd1990


People also ask

How would you check if a number is an integer?

The Number. isInteger() method returns true if a value is an integer of the datatype Number. Otherwise it returns false .

How do you check if it is a number in JavaScript?

In JavaScript, there are two ways to check if a variable is a number : isNaN() – Stands for “is Not a Number”, if variable is not a number, it return true, else return false. typeof – If variable is a number, it will returns a string named “number”.

How do you check if a value is an integer in JavaScript?

The Number. isInteger() method in JavaScript is used to check whether the value passed to it is an integer or not. It returns true if the passed value is an integer, otherwise, it returns false.

Is numeric function in JavaScript?

Definition and UsageThe isNaN() method returns true if a value is NaN. The isNaN() method converts the value to a number before testing it.


1 Answers

Apparently, IE treats DOM objects and Javascript objects separately, and you can't extend the DOM objects using Object.prototype.

IE doesn't let you use a prototype that is not native..

You'll have to make a separate function (global if you want) as

function isInteger(num) {
  return (num ^ 0) === num;
}

console.log(isInteger(1));
like image 176
Muhammad Ahsan Ayaz Avatar answered Sep 17 '22 21:09

Muhammad Ahsan Ayaz