How is the ternary operator evaluated in JavaScript?

前端 未结 4 1085
猫巷女王i
猫巷女王i 2021-02-19 06:19

Regarding the ternary (? :) operator in JavaScript, I would like to know how it is evaluated by a typical browser\'s JavaScript interpreter:

Alternative A:<

4条回答
  •  温柔的废话
    2021-02-19 06:28

    Run this and find out:

    function bool() {
        alert('bool');
        return false;
    }
    
    function a() {
        alert('a');
        return 'A';
    }
    
    function b() {
        alert('b');
        return 'B';
    }
    
    alert(bool() ? a() : b())
    

提交回复
热议问题