Javascript 具有多个return语句的函数返回什么?

Javascript 具有多个return语句的函数返回什么?,javascript,return,return-value,Javascript,Return,Return Value,这是出自第一本JavaScript编程书的练习 function findCarInLot(car) { for (var i = 0; i < lot.length; i++) { if (car === lot[i]) { return i; } } return -1; } v

这是出自第一本JavaScript编程书的练习

    function findCarInLot(car) {
            for (var i = 0; i < lot.length; i++) {
                if (car === lot[i]) {
                    return i;
                }
            }
            return -1;
        }

    var lot = [chevy, taxi, fiat1, fiat2];
那么,当函数完成时,它不会返回吗?或者当它被否定的时候

    return i;

发生了什么?还是两个值都返回?有没有人能帮我解决一下这个问题,告诉我这里的规则是什么。

根据它击中的是哪一个回击,这将是它返回的唯一东西

function findCarInLot(car) {
            for (var i = 0; i < lot.length; i++) {
                if (car === lot[i]) {
                    return i; // If this if statement is true I will return here and this function will end and I will never make it to the next return
                }
            }
            return -1; // This will only get called if the above if statement is false
        }

    var lot = [chevy, taxi, fiat1, fiat2];
我们跑

console.log(findCarInLot("fiat1")); // This will return 2 (Third element of the array)
这是从if语句中的
returni
返回的,您永远不会看到
return-1

但如果我们这样做了

console.log(findCarInLot("lamadamadingdong")); // This will return -1 as it was never found in the array of cars

希望这有帮助。

根据它击中的是哪一个返回,这将是它返回的唯一内容

function findCarInLot(car) {
            for (var i = 0; i < lot.length; i++) {
                if (car === lot[i]) {
                    return i; // If this if statement is true I will return here and this function will end and I will never make it to the next return
                }
            }
            return -1; // This will only get called if the above if statement is false
        }

    var lot = [chevy, taxi, fiat1, fiat2];
我们跑

console.log(findCarInLot("fiat1")); // This will return 2 (Third element of the array)
这是从if语句中的
returni
返回的,您永远不会看到
return-1

但如果我们这样做了

console.log(findCarInLot("lamadamadingdong")); // This will return -1 as it was never found in the array of cars

希望这能有所帮助。

这很有帮助。我正在学习JavaScript的一致性。很高兴知道这里有一个坚实的规则,哈哈。这很有帮助。我正在学习JavaScript的一致性。很高兴知道这里有一个坚实的规则哈哈。