3 ms·
The code is either going to loop once and exit or loop forever no
by darepublic 2y ago
The code is either going to loop once and exit or loop forever no
- andrewf 2y agoPutting this in my web console: let susJsonString=JSON.stringify(JSON.stringify(JSON.stringify({foo:1}))) console.log("initial:", susJsonString); try { while(typeof susJsonString==='string') { susJsonString = JSON.parse(susJsonString); console.log("iteration:", typeof susJsonString, susJsonString); } } catch { susJsonString = {}; } I see: initial: "\"{\\\"foo\\\":1}\"" iteration: string "{\"foo\":1}" iteration: string {"foo":1} iteration: object {foo: 1} A comment explaining the sort of "sus" input it was designed to cope with may have been helpful.
- accoil 2y agoIt will stop when it gets something that's not a string due to while(typeof susJsonString==='string') { susJsonString = JSON.parse(susJsonString); as it'll keep reassigning and parsing until gets a non string back (or alternatively error out if the string is not valid json)