0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

elseを使わない条件分岐

Posted at

##はじめに
returnの概念で沼にハマったので記載

##以下は同じ


var number = "1"
  if( number == "1"){
    return true;
  }else{
    return false;
  }
//true

var number = "1"
  if( number == "1"){
    return true;
  }
  return false;
//true

##理由
上はifを使った単純な条件分岐です。
今までしっくり来ていなかったのは下なのですが、
return が来るとその時点でScriptが終わってしまうためです。

下の場合でいうと、
数字が1なのでそのまま進みreturn trueを発火させて終わりですが、
数字が1以外の場合は、return trueをスキップするため、Scriptが終わらず
return false で終わることになります。

##検証用


function demo() {
  
  var number = "2"
  
  if( number == "1"){
    return true;
  }
  return false;
  
}

Logger.log(demo());//上のfunction demoという関数の結果をログ出力する
0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?