「それ、もっとスマートに書けるよ」をもしもぼくが書くなら
ある特定の文字列が別の文字列内にあるかどうか
const ua = navigator.userAgent; if (~ua.indexOf('iPhone') || ~ua.indexOf('iPod') || ~ua.indexOf('iPad')) { return 'ios'; } else { return 'other'; }
正規表現を使います
const isAppleDevice = !!navigator.userAgent.match(/iPhone|iPod|iPad/)
それlet使わなくてもできるよ
const { foo, bar } = (() => { if (new Date().getHours() < 12) { return { foo: 'forenoon', bar: 'am' } } else { return { foo: 'afternoon', bar: 'pm' } } })();
基本は別関数にして返す形にしますが、この例ならこうするかなと。
const isForenoon = new Date().getHours() < 12
const [ foo, bar ] = isForenoon ? ['forenoon','am'] : ['afternoon','pm']