本文介绍了当您使用或"||"返回值时会发生什么?操作员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不想从此代码中获取或想要任何东西.我只是对以下代码的一部分感到困惑.作者使用或 || 运算符返回了一个值.我的问题是,将返回哪个值以及何时返回?

I'm not trying to get or want anything from this code. I just have a confusion with a part of the following code. The author has returned a value with or || operator. my question is which value will be returned and when?

function findSolution (target) {
    function find (current, history) {
        if (current == target) {
            return history
        }else if (current > target) {
            return null
        } else {
            return find (current + 5, `${history} + 5)`) ||
                    find(current * 3, `(${history} * 3)`)
        }
    }
    return find(1, '1')
}

console.log(findSolution(24))

这部分代码困扰着我

return find (current + 5, `${history} + 5)`) ||
       find(current * 3, `(${history} * 3)`)

任何人都可以告诉我,返回时该值是多少?为什么?

can anyone please tell me, which value when return? and why?

推荐答案

来自 MDN :

换句话说,您可以将其视为如下功能:

in other words, you can see it as a function like this:

const or = (fel, sel) =>{
     if(fel) return fel;
     return sel;
}

这就是为什么发生这种情况:

That's why this happens:

const x = 0 || 1; // x == 1
// same as:
//
// const or = (fel, sel) =>{
//      if(fel) return fel;
//      return sel;
// }
// const x = or(0,1)

这篇关于当您使用或"||"返回值时会发生什么?操作员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 13:12