I have troubles with a variable in JavaScript that I want to be int
.
In the beginning, it looked like an int
, then I did some diagnostics and found out it is not.
Ultimately, I want to cast it to int
.
This is the code I used:
console.log(variable);
var isInt = variable % 1 === 0;
console.log('The variable is int?');
console.log(isInt);
var isFloat = +variable && variable !== (variable|0);
console.log('The variable is float?');
console.log(isFloat);
And these are the results:
2,365
The variable is int?
false
The variable is float?
NaN
The line for checking if the variable is float
I got it from this question:
How do I check that a number is float or integer?
It is the second answer.
NaN
means my variable is probably a string
?
EDIT: My question is different from the one offered as a solution, as i am not sure if my variable is float or integer, thus i am trying to identify it first. I picked parts from answers from that question, but it did nto work.