如何在javascript中的箭头函数中使用if-else conditon? [英] how to use if-else conditon in arrow function in javascript?

查看:230
本文介绍了如何在javascript中的箭头函数中使用if-else conditon?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是java脚本中Arrow函数的新手。任何人请告诉我
是否可以使用箭头函数在java脚本中使用if else条件。

I am new for Arrow function in java script. anybody please let me know is it possible to use if else condition in java script using arrow function.

推荐答案

箭头函数可以简单地看作是常规函数的简洁版本,除了暗示 return (在其他一些微妙的事情中你可以读到这里)。使用if / else的一个好方法是三元。采用这个常规函数:

An arrow function can simply be seen as a concise version of a regular function, except that the return is implied (among a few other subtle things you can read about here). One nice way to use an if/else is though a ternary. Take this regular function:

function(a){
    if(a < 10){
        return 'valid';
    }else{
        return 'invalid';
    }
}

使用三元组的箭头函数中的等价物是:

The equivalent in an arrow function using a ternary is:

a => (a < 10) ? 'valid' : 'invalid'

这篇关于如何在javascript中的箭头函数中使用if-else conditon?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆