Picking a time based on when the app was last used I am trying to set up a time variable as a local storage property to be retrieved when I open my app in my browser.

If I open it for the first time, today, I'll set it up to today at midnight. 

Otherwise, if the time property has already been set up today in a previous session, I'll provide the retrieved value to the session, and assign Date.now to the local storage property for next time I'll visit my app.

I am trying to use ternary operators in place of if statements, but I see duplicated code I am not able to avoid. I am sure I am missing a trivial logic error, but I am not seeing it right now.

const NotificationsStorage: { lastCheck: number } = JSON.parse(localStorage.getItem('NotificationsStorage'));
const today: number = moment().startOf('day').valueOf();
const now: number = moment().valueOf();

localStorage
    ? NotificationsStorage
        ? (NotificationsStorage.lastCheck &amp;&amp; NotificationsStorage.lastCheck &gt; today)
            ? (
                this.lastCheck = NotificationsStorage.lastCheck,
                localStorage.setItem('NotificationsStorage', JSON.stringify({ lastCheck: now }))
            )
            : (
                localStorage.setItem('NotificationsStorage', JSON.stringify({ lastCheck: today })),
                this.lastCheck = today
            )
        : (
            localStorage.setItem('NotificationsStorage', JSON.stringify({ lastCheck: today })),
            this.lastCheck = today
        )
    : console.warn('Current browser doesn\'t support Local Storage API');
Whether you use ternaries or classic if/else you can often make your nesting easier to read by inverting your conditions. i.e.

! window.localStorage
    ? console.warn('Current browser doesn\'t support Local Storage API');             
    : NotificationsStorage
    &amp;&amp; NotificationsStorage.lastCheck 
    &amp;&amp; NotificationsStorage.lastCheck &gt; today
        ? (
            this.lastCheck = NotificationsStorage.lastCheck,
            localStorage.setItem('NotificationsStorage', JSON.stringify({ lastCheck: now }))
        )
        : (
            localStorage.setItem('NotificationsStorage', JSON.stringify({ lastCheck: today })),
            this.lastCheck = today
        )


Although you can't do it with a ternary operator, I also usually prefer to use early returns to limit nesting. This also avoids the issue that you access localStorage in your first line even though it might not exist.

if (! window.localStorage) {
  console.warn('Current browser doesn\'t support Local Storage API');  
  return;
}

const NotificationsStorage: { lastCheck: number } = JSON.parse(localStorage.getItem('NotificationsStorage'));
const today: number = moment().startOf('day').valueOf();
const now: number = moment().valueOf(); 

if (NotificationsStorage
    &amp;&amp; NotificationsStorage.lastCheck 
    &amp;&amp; NotificationsStorage.lastCheck &gt; today) {
      this.lastCheck = NotificationsStorage.lastCheck,
      localStorage.setItem('NotificationsStorage', JSON.stringify({ lastCheck: now }))
 } else { 
      localStorage.setItem('NotificationsStorage', JSON.stringify({ lastCheck: today })),
      this.lastCheck = today
}


You could simply that last if by separating the variables and the logic to update them:

const {newStoredValue, newLocalValue} = 
    NotificationsStorage
    &amp;&amp; NotificationsStorage.lastCheck 
    &amp;&amp; NotificationsStorage.lastCheck &gt; today 
    ? [now, NotificationsStorage.lastCheck]
    : [today, today];

 localStorage.setItem('NotificationsStorage', JSON.stringify({ lastCheck: newStoredValue }));
 this.lastCheck = newLocalValue;