A Quick, Helpful Built-In When Working With Mathematic Operations.
NaN is an abbreviation, rather an acronym, of “Not a Number”, and is not to be confused with the delicious flatbread: naan.
NaN is rarely used in writing programs or scripts. In modern web browsers it is a non-configureable and non-writeable property. It is most often received as a return value when a mathematic (Math) function fails, or when a function fails to parse a number, e.g.
NaN is quirky and uncommon, but is occasionally presented and needs to be handled.
In a recent project, with a
React client side, the user account included a portfolio of collected stocks and corresponding prices. The portfolio had to include a gross sum of total earnings. Sounds easy enough: gather all the stock prices from the portfolio and add them together. Of course, things are never quite as easy as first assumed to be.
Upon a returning user logging into their account, the state was loaded with that user’s stocks, but the prices of these stocks—and, consequently, the total earnings—were dynamically rendered by fetching to an external
API for current market prices. The difficulty was properly timing and dispatching actions to fetch data from the server side database and external
API while properly rendering components on the client side. Within that chain of events, an action is dispatched which calculates the gross sum of the user’s total earnings, resolved by a function called
Best solutions were to first properly sequence the fetches, promises, and component renders, or craft the
setTotalEarnings function to better control the return value, or more simply handle a falsy return value while conditionally rendering, but, as I was building this application in a short time frame for an assessment, I pushed ahead and addressed what was needed to just get the thing working in time! Long story short, the
setTotalEarnings function was intermittently returning
NaN, and I was having trouble handling it to be rendered correctly in the appropriate
Build your eCommerce project by hiring our expert eCommerce Website developers. Our Dedicated Web Designers develop powerful & robust website in a short span of time.
Contact DataPierce for any questions or queries you have about website development, designing, marketing projects or any small or enterprise software development.
Best Mobile App Development Company India, WebClues Global is one of the leading web and mobile app development company. Our team offers complete IT solutions including Cross-Platform App Development, CMS & E-Commerce, and UI/UX Design.
Do you want excellent and world class web development services for your valuable projects? Contact **RB Genie **now, we have more than 8 years experienced team of web developers, which specializes in overall web design and website development...