javascriptshorthandshorthand-if

shorthand if/else variable exists


I'm simply running a function which checks if the variable year is set if not then set it new Date().getFullYear().

The error I get:

Uncaught ReferenceError: year is not defined

year = (year) ? year : new Date().getFullYear();
console.log(year);

Why can't I check if year exists and if not set it?


Solution

  • year = year || new Date().getFullYear();

    Useful to check function parameters