var s = '';
var num = parseInt(s) || 0;
When not used with boolean values, the logical OR || operator returns the first expression parseInt(s) if it can be evaluated to true, otherwise it returns the second expression 0. The return value of parseInt('') is NaN. NaN evaluates to false, so num ends up being set to 0.
var s = '';
var num = parseInt(s) || 0;
When not used with boolean values, the logical OR || operator returns the first expression parseInt(s) if it can be evaluated to true, otherwise it returns the second expression 0. The return value of parseInt('') is NaN. NaN evaluates to false, so num ends up being set to 0.
You can also use the isNaN() function:
var s = ''
var num = isNaN(parseInt(s)) ? 0 : parseInt(s)
Various ways to coerse JS strings to numbers, and their consequences:

(source: phrogz.net)
I personally use *1 as it is short to type, but still stands out (unlike the unary +), and either gives me what the user typed or fails completely. I only use parseInt() when I know that there will be non-numeric content at the end to ignore, or when I need to parse a non-base-10 string.
Edit: Based on your comment, if using phone.val() fixed it then
- You were using jQuery (which you never mentioned, and should have), and
- You actually had/have a jQuery object, wrapping one or more DOM elements (probably just one).
Whenever you do var foo = $('…'); then the foo variable references a jQuery object of one or more elements. You can get the first actual DOM element from this via var fooEl = foo[0]; or var fooEl = foo.get(0);…but even then you still have a DOM element and not a particular property of that.
For form inputs, you need to get the .value from the DOM element, which is what the jQuery .val() method does.
parseInt is a bit odd at times:
Copy> parseInt("123-456-789")
123
Fortunately you can probably solve your case with:
Copy> Number("123-456-789")
NaN
I am trying to convert a query parameter to integer if it is a purely numeric string like '1', but when I make ?limit='1', it returns NaN. Also note that even the value received by the method is '1' and type is string.
However, when I do parseInt('1') browser's console it return 1.
Node Browser's consoleTry this code:
isNaN(parseFloat("geoff"))
For checking whether any value is NaN, instead of just numbers, see here: How do you test for NaN in JavaScript?
I just came across this technique in the book Effective JavaScript that is pretty simple:
Since NaN is the only JavaScript value that is treated as unequal to itself, you can always test if a value is NaN by checking it for equality to itself:
var a = NaN;
a !== a; // true
var b = "foo";
b !== b; // false
var c = undefined;
c !== c; // false
var d = {};
d !== d; // false
var e = { valueOf: "foo" };
e !== e; // false
Didn't realize this until @allsyed commented, but this is in the ECMA spec: https://tc39.es/ecma262/multipage/global-object.html#sec-isnan-number
No, parseInt() allows "garbage" (non-number) characters after some numeric characters. The parseFloat() function has the same "feature".
If you want to get a number, you can use Number("123") or simply the + unary operator. However those accept any JavaScript number, including ones with fractional parts or numbers in scientific notation. But those methods will fail and give NaN if the string input is not a valid numeric constant.
You could do something like
if (+someString === Math.floor(+someString))
I guess.
edit — a comment notes that you'd also want to check the degenerate case of an empty or all-space string too. A simple regular expression (/^\d+$/) followed by a sanity check that it's not 200 digits long (amonth possibly other things) is another alternative.
If you were wanting to check NaNanother way would be to test it with isNaN().
isNaN() will return a boolean giving you a verification for a given input.
parseInt('5aab4') //5
isNaN('5aab4') // true
If I'm understanding this correctly, the code above is equivalent to:
var bucketId = '"17"';
console.log(bucketId);
var bucketIdNumber = parseInt(bucketId, 10);
console.log("bucketIdNumber " + bucketIdNumber);
In which case, bucketIdNumber does return NaN. You will need to further parse this cookie, and remove any inner quotation marks.
bucketId probably doesn't start with an integer.
If the first character cannot be converted to a number, parseInt() returns NaN.