Help us understand the problem. What is going on with this article?

もしぼくが「それ、もっとスマートに書けるよ」を書くなら

More than 1 year has passed since last update.

それ、もっとスマートに書けるよ」をもしもぼくが書くなら

ある特定の文字列が別の文字列内にあるかどうか

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']

参考

それ、もっとスマートに書けるよ

「それ、もっとスマートに書けるよ」がもっとスマートに書けるよ

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした