Type checking in javascript

How to check if a variable is currently an integer type? I was looking for some kind of resource for this, and I think the === operator is important, but I'm not sure how to check if the variable is Integer (or Array, for that matter)

+52
javascript types
Dec 22 '10 at 23:18
source share
8 answers

A variable will never be an integer type in JavaScript - it does not distinguish between different types of Number.

You can check if the variable contains a number, and if that number is an integer.

(typeof foo === "number") && Math.floor(foo) === foo 

If the variable can be a string containing an integer, and you want to find out if this has a value:

 foo == parseInt(foo, 10) 
+88
Dec 22 '10 at 23:21
source share

A number is an integer if its modulo% 1 is 0 -

 function isInt(n){ return (typeof n== 'number' && n%1== 0); } 

This is as good as javascript - say + - from ten to fifteen.

isInt (Math.pow (2,50) +. 1) returns true, like

Math.pow (2.50) +. 1 == Math.pow (2,50) // true

+6
Dec 23 2018-10-12T00:
source share

These days ECMAScript 6 (ECMA-262) is in the house. Use Number.isInteger(x) to ask the question you want to ask regarding type x:

 js> var x = 3 js> Number.isInteger(x) true js> var y = 3.1 js> Number.isInteger(y) false 
+6
Aug 16 '15 at 17:14
source share
 var a = 1; if (typeof a == 'number') { // ... } 
+1
Dec 22 '10 at 23:20
source share

Try this code:

alert (typeof (1) == "number");

0
Dec 22 '10 at 23:22
source share

I know that you are interested in Integer numbers, so I won’t answer that, but if you ever want to check the floating point numbers, you can do it.

 function isFloat( x ) { return ( typeof x === "number" && Math.abs( x % 1 ) > 0); } 

Note. This MAY handle numbers ending in .0 (or any logically equivalent number 0 ) as INTEGER. This actually requires a floating point precision error to detect floating point values ​​in this case.

Ref.

 alert(isFloat(5.2)); //returns true alert(isFloat(5)); //returns false alert(isFloat(5.0)); //return could be either true or false 
0
Oct 23 '13 at 17:56
source share

In general, several utility libraries, such as YourJS , offer functions to determine if something is an array or something integer or many other types. YourJS determines isInt , checking if this number is a number, and then if it is divisible by 1:

 function isInt(x) { return typeOf(x, 'Number') && x % 1 == 0; } 

The above snippet was taken from this snippet of your site and therefore only works because typeOf is defined by the library. You can download a minimalistic version of YourJS, which basically has only type checking functions like typeOf() , isInt() and isArray() : http://yourjs.com/snippets/build/34,2

0
Nov 22 '15 at 22:36
source share

You can also see Runtyper , a tool that checks the types of operands in === (and other operations).
For your example, if you have a strict comparison of x === y and x = 123, y = "123" , it automatically checks for typeof x, typeof y and displays a warning in the console:

Strict comparison of different types: 123 (number) === "123" (string)

0
Apr 01 '17 at 8:02
source share



All Articles