JSON.parse fallback for undefined array to avoid exceptionDetecting an undefined object propertyCreate ArrayList from arrayHow do I check if an array includes an object in JavaScript?How to append something to an array?event.preventDefault() vs. return falseHow to determine if variable is 'undefined' or 'null'?Loop through an array in JavaScriptHow to check for “undefined” in JavaScript?How do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?

What game is this character in the Pixels movie from?

Why do changes to /etc/hosts take effect immediately?

Boolean Difference with Offset?

Why do we use a cylinder as a Gaussian surface for infinitely long charged wire?

Most important new papers in computational complexity

If two black hole event horizons overlap (touch) can they ever separate again?

Balanced parentheses using STL C++

Can I travel from Germany to England alone as an unaccompanied minor?

Could a Weapon of Mass Destruction, targeting only humans, be developed?

Can two or more lightbeams (from a laser for example) have visible interference when they cross in mid-air*?

Buliding a larger matrix from a smaller one

Is this homebrew Half-Phoenix race balanced?

Find first and last non-zero column in each row of a pandas dataframe

Are all commands with an optional argument fragile?

How to test CRUD and FLS in unit tests?

How can I write a panicked scene without it feeling like it was written in haste?

Is it bad to describe a character long after their introduction?

How to securely dispose of a smartphone?

Is it okay to fade a human face just to create some space to place important content over it?

Meaning of じゃないんじゃない?

How do I tell the reader that my character is autistic in Fantasy?

Matrix decomposition

How did installing this RPM create a file?

Different budgets within roommate group



JSON.parse fallback for undefined array to avoid exception


Detecting an undefined object propertyCreate ArrayList from arrayHow do I check if an array includes an object in JavaScript?How to append something to an array?event.preventDefault() vs. return falseHow to determine if variable is 'undefined' or 'null'?Loop through an array in JavaScriptHow to check for “undefined” in JavaScript?How do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








-1















I'm parsing my array and everything is OK if it is defined:



JSON.parse(myArray);


However I get an exception in case if myArray is undefined.



What is the best fallback for it, is there anything better than this:



JSON.parse(myArray || '[]');


similar like we first verify the object to avoid an exception if undefined



if (obj) 
//do something with obj.something



So, is there anything shorter than



JSON.parse(myArray || '[]'); 


Thank you.










share|improve this question

















  • 2





    That seems pretty short as it is.

    – Pointy
    Mar 25 at 13:17











  • That's pretty much as good as you'll get short of modifying the lib function to check undefined

    – SoZettaSho
    Mar 25 at 13:18

















-1















I'm parsing my array and everything is OK if it is defined:



JSON.parse(myArray);


However I get an exception in case if myArray is undefined.



What is the best fallback for it, is there anything better than this:



JSON.parse(myArray || '[]');


similar like we first verify the object to avoid an exception if undefined



if (obj) 
//do something with obj.something



So, is there anything shorter than



JSON.parse(myArray || '[]'); 


Thank you.










share|improve this question

















  • 2





    That seems pretty short as it is.

    – Pointy
    Mar 25 at 13:17











  • That's pretty much as good as you'll get short of modifying the lib function to check undefined

    – SoZettaSho
    Mar 25 at 13:18













-1












-1








-1








I'm parsing my array and everything is OK if it is defined:



JSON.parse(myArray);


However I get an exception in case if myArray is undefined.



What is the best fallback for it, is there anything better than this:



JSON.parse(myArray || '[]');


similar like we first verify the object to avoid an exception if undefined



if (obj) 
//do something with obj.something



So, is there anything shorter than



JSON.parse(myArray || '[]'); 


Thank you.










share|improve this question














I'm parsing my array and everything is OK if it is defined:



JSON.parse(myArray);


However I get an exception in case if myArray is undefined.



What is the best fallback for it, is there anything better than this:



JSON.parse(myArray || '[]');


similar like we first verify the object to avoid an exception if undefined



if (obj) 
//do something with obj.something



So, is there anything shorter than



JSON.parse(myArray || '[]'); 


Thank you.







javascript arrays undefined fallback






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 25 at 13:15









HaradzieniecHaradzieniec

4,06620 gold badges86 silver badges174 bronze badges




4,06620 gold badges86 silver badges174 bronze badges







  • 2





    That seems pretty short as it is.

    – Pointy
    Mar 25 at 13:17











  • That's pretty much as good as you'll get short of modifying the lib function to check undefined

    – SoZettaSho
    Mar 25 at 13:18












  • 2





    That seems pretty short as it is.

    – Pointy
    Mar 25 at 13:17











  • That's pretty much as good as you'll get short of modifying the lib function to check undefined

    – SoZettaSho
    Mar 25 at 13:18







2




2





That seems pretty short as it is.

– Pointy
Mar 25 at 13:17





That seems pretty short as it is.

– Pointy
Mar 25 at 13:17













That's pretty much as good as you'll get short of modifying the lib function to check undefined

– SoZettaSho
Mar 25 at 13:18





That's pretty much as good as you'll get short of modifying the lib function to check undefined

– SoZettaSho
Mar 25 at 13:18












1 Answer
1






active

oldest

votes


















2














Your current method works just fine as well. I don't really see a reason to change it but if you feel you need to two options come to mind:



First, you could initialize myArray with it defaulted to an empty array before it gets its values assigned.



var myArray = '[]';


Otherwise if myArray is a parameter passed to a method you are parsing it from, you can default it in the arguments section.



function dosomething(myArray = '[]') 
JSON.parse(myArray);






share|improve this answer




















  • 1





    right, but it should be myArray = "[]", not just []

    – georg
    Mar 25 at 14:25










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55338665%2fjson-parse-fallback-for-undefined-array-to-avoid-exception%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














Your current method works just fine as well. I don't really see a reason to change it but if you feel you need to two options come to mind:



First, you could initialize myArray with it defaulted to an empty array before it gets its values assigned.



var myArray = '[]';


Otherwise if myArray is a parameter passed to a method you are parsing it from, you can default it in the arguments section.



function dosomething(myArray = '[]') 
JSON.parse(myArray);






share|improve this answer




















  • 1





    right, but it should be myArray = "[]", not just []

    – georg
    Mar 25 at 14:25















2














Your current method works just fine as well. I don't really see a reason to change it but if you feel you need to two options come to mind:



First, you could initialize myArray with it defaulted to an empty array before it gets its values assigned.



var myArray = '[]';


Otherwise if myArray is a parameter passed to a method you are parsing it from, you can default it in the arguments section.



function dosomething(myArray = '[]') 
JSON.parse(myArray);






share|improve this answer




















  • 1





    right, but it should be myArray = "[]", not just []

    – georg
    Mar 25 at 14:25













2












2








2







Your current method works just fine as well. I don't really see a reason to change it but if you feel you need to two options come to mind:



First, you could initialize myArray with it defaulted to an empty array before it gets its values assigned.



var myArray = '[]';


Otherwise if myArray is a parameter passed to a method you are parsing it from, you can default it in the arguments section.



function dosomething(myArray = '[]') 
JSON.parse(myArray);






share|improve this answer















Your current method works just fine as well. I don't really see a reason to change it but if you feel you need to two options come to mind:



First, you could initialize myArray with it defaulted to an empty array before it gets its values assigned.



var myArray = '[]';


Otherwise if myArray is a parameter passed to a method you are parsing it from, you can default it in the arguments section.



function dosomething(myArray = '[]') 
JSON.parse(myArray);







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 28 at 12:18

























answered Mar 25 at 13:21









Daniel DeesDaniel Dees

1372 silver badges11 bronze badges




1372 silver badges11 bronze badges







  • 1





    right, but it should be myArray = "[]", not just []

    – georg
    Mar 25 at 14:25












  • 1





    right, but it should be myArray = "[]", not just []

    – georg
    Mar 25 at 14:25







1




1





right, but it should be myArray = "[]", not just []

– georg
Mar 25 at 14:25





right, but it should be myArray = "[]", not just []

– georg
Mar 25 at 14:25








Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55338665%2fjson-parse-fallback-for-undefined-array-to-avoid-exception%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript