js判断语句的写法规则

JavaScript011

js判断语句的写法规则,第1张

在写JS中最常见的就是 if 判断,但是直接写 if(){}else{}太low ,下面是常见的几种js的if判断的写法:

/**

* js 判断的几种写法

*/

var a = 10,b = 20

console.log(a)

console.log(b)

/*最直接*/

if(a >b){

console.log('a大')

}else{

console.log('b大')

}

/*改变1*/

if(a >b) console.log('a大')

if(a <b) console.log('b大')

/*改变2*/

if(a >b) console.log('a大')

else console.log('b大')

/*最简单*/

console.log(a>b ? 'a大' : 'b大')

用三目运算符。

使用ng-switch结构。

使用ng-if。

angularjs中的判断语句说明。

由于angularjs不支持if-else结构,所以判断的写法如下:

用三目运算符:

<span>{{isLarge ? 'video.large' : 'video.small'}}</span>

使用ng-switch结构:

<div ng-switch on="video">

<div ng-switch-when="video.large">

</div>

<div ng-switch-default>

<!-- code to render the regular video block -->

</div>

</div>

使用ng-if(angularjs 1.1.5以后版本有效):

<div ng-if="video == video.large">

<!-- code to render a large video block-->

</div>

<div ng-if="video != video.large">

<!-- code to render the regular video block -->

</div>