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.

Answer from Matthew on Stack Overflow
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
How does 'parseInt("") != NaN' evaluate to true? - JavaScript - The freeCodeCamp Forum
June 19, 2022 - I’ve tried this in the console, and it doesn’t make sense. parseInt("") evaluates to NaN. A falsy value So why does parseInt("") != NaN evaluate to true? NaN != NaN also evaluates to true For the record, the same happens when you use the ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › parseInt
parseInt() - JavaScript | MDN
The parseInt function converts its first argument to a string, parses that string, then returns an integer or NaN. If not NaN, the return value will be the integer that is the first argument taken as a number in the specified radix.
🌐
Reddit
reddit.com › r/node › parseint returning nan even for purely numeric strings
r/node on Reddit: parseInt returning NaN even for purely numeric strings
March 30, 2024 -

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 console

🌐
DEV Community
dev.to › darkmavis1980 › you-should-stop-using-parseint-nbf
You should stop using `parseInt()` - DEV Community
October 15, 2021 - The Number(string) function evaluate the full string and converts it to a string, and if the string is not a number it will just return NaN. While parseInt(string, [radix]) will try to find the first number in the string passed, and convert ...
🌐
Deano
deano.me › home › javascript: parseint() tip to avoid nan
JavaScript: parseInt() Tip to avoid NaN
August 20, 2020 - There is a function that can check for NaN called isNaN() but I have another approach which is cleaner and simpler to use throughout your code. var myStr = "NotaNumber"; var myNum = 3; myNum += parseInt(myStr) || 0;
Address   22 Avenue Road, WS12 2DY, Cannock
🌐
Medium
medium.com › @vdsnini › why-1-9-3-map-parseint-returns-1-nan-nan-in-javascript-5c21cf24d4dc
Why [‘1’, ‘9’, ‘3’].map(parseInt) returns [1, NaN, Nan] in Javascript | by Beenish Khan | Medium
May 31, 2024 - Third Iteration: Element: ‘3’ Index: 2 Call: parseInt(‘3’, 2) Result: NaN Explanation: The radix 2 means binary. The string ‘3’ is not a valid binary number.
🌐
Medium
medium.com › @cristi.nord › javascript-parseint-c6b2a271f153
JavaScript — parseInt()
July 21, 2019 - The parseInt function converts its first argument to a string, parses that string, then returns an integer or NaN.
Find elsewhere
🌐
HashBangCode
hashbangcode.com › article › something-be-aware-javascript-isnan
Something To Be Aware Of JavaScript isNaN | #! code
To solve this problem you can use the parseInt() function. This takes a string as an input and tries to convert it into a number, if this is not possible then it returns NaN. If parseInt() is given an empty string or a null value it will also return NaN, rather than converting them to 0.
🌐
O'Reilly
oreilly.com › library › view › javascript-the-definitive › 0596101996 › re150.html
JavaScript: The Definitive Guide, 5th Edition
August 17, 2006 - The parsed number, or NaN if s does not begin with a valid integer. In JavaScript 1.0, parseInt( ) returns 0 instead of NaN when it cannot parse s.
Author   David Flanagan
Published   2006
Pages   1018
🌐
Sololearn
sololearn.com › en › Discuss › 3034526 › why-at-project-javascript-parseint-always-return-nan
Why at project javascript parseInt() always return NaN | Sololearn: Learn to code for FREE!
Muhammad Ilham If you try to parse a string value to number then it will always give you NaN. For example: parseInt('xyz') = NaN parseInt('xy1234') = NaN If string containing number first then it will be parse.
🌐
LoginRadius
loginradius.com › home
NaN in JavaScript: An Essential Guide
November 22, 2019 - These two different strings being passed to parseInt() will both return NaN. ... Both statements return NaN, but are they really the same? Maybe, but it certainly makes sense why JavaScript would disagree, given that they are derived from different string arguments.
🌐
Medium
medium.com › dailyjs › parseint-mystery-7c4368ef7b21
Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in Javascript | by Eric Tong | DailyJS | Medium
July 11, 2019 - Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in Javascript Javascript is weird. Don’t believe me? Try converting an array of strings into integers using map and parseInt. Fire up your …
🌐
Reddit
reddit.com › r/programming › why ['1', '7', '11'].map(parseint) returns [1, nan, 3] in javascript
r/programming on Reddit: Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in Javascript
June 24, 2019 - In order: the current value; the ... too many argument just works. Obviously parsing a radix of 1 means that any number that is not 0 can't be expressed so we get NaN and "11" with a radix of 2 of decimal "3"....
🌐
Cullen Web Services
cullenwebservices.com › home › javascript numbers: number(), parseint(), parsefloat()
JavaScript Numbers: Number(), parseInt(), parseFloat() - Cullen Web Services
December 11, 2015 - Number(true); //1 Number(false); //0 Number(13); //13 Number("13"); //13 var b; Number(b); //Number(undefined) NaN var b=10; Number(b); //10 Number("1.5"); //1.5 Number("01.5"); //1.5 Number("0xA"); //10 Number(""); //0 Number("cindy"); //NaN Number("123cindy"); //NaN Number("123.123.123"); //NaN Number("4.89e7"); //48900000 · parseInt(true); //NaN parseInt(false); //NaN parseInt(13); //13 parseInt("13"); //13 var k; //k is undefined parseInt(k); //NaN var b=10; parseInt(b); //10 parseInt("1.5") //1 parseInt("01.5") //1 parseInt("0xA"); //10 parseInt(""); //NaN parseInt("cindy"); //NaN parseInt("123cindy") //123 parseInt("123.123.123") //123 parseInt("4.89e7") //4