Monday, June 3, 2024
190
rated 0 times [  193] [ 3]  / answers: 1 / hits: 38231  / 9 Years ago, mon, january 4, 2016, 12:00:00

I'm wallowing in ES2015+ luxury with a few projects right now and am wondering whether I can get rid of the much hated crutch to check for undefined in the new wonderland.



Is there a shorter but still exact way to typeof varName === 'undefined' in ES2015+ already?



Of course I could use default parameters but this also feels like an unnecessary assignment.



function coolFn(a = null){
if (a===null) console.log(no a supplied);
}

More From » ecmascript-6

 Answers
193

Just check for varName === undefined.



In older browsers it was possible to assign an alternate value to the global undefined variable causing that test to fail, but in ES2015+ that's now impossible.



Note that there's no way to distinguish explicitly passing undefined as a parameter from leaving the parameter out altogether other than by looking at arguments.length.


[#63846] Saturday, January 2, 2016, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jarrodfletchers

Total Points: 75
Total Questions: 94
Total Answers: 95

Location: Netherlands
Member since Thu, Jul 1, 2021
3 Years ago
jarrodfletchers questions
Sat, Sep 25, 21, 00:00, 3 Years ago
Mon, Jan 21, 19, 00:00, 5 Years ago
Sun, Dec 16, 18, 00:00, 6 Years ago
Sun, Nov 4, 18, 00:00, 6 Years ago
;