An approach using a try/catch
function getFormatedDategetFormattedDate(date = new Date()) { try { date.toISOString(); } catch (e) { date = new Date(); } return date; } console.log(getFormatedDategetFormattedDate()); console.log(getFormatedDategetFormattedDate('AAAA')); console.log(getFormatedDategetFormattedDate(new Date('AAAA'))); console.log(getFormatedDategetFormattedDate(new Date(2018, 2, 10)));