最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Function returns undefined with ternary operator - Stack Overflow

matteradmin5PV0评论

I am trying to wrap my head around the isBreadwinner function. Why does not true pass into the (**) function. Why is Earning member:undefined

<script>
    "use strict"

    //creating a user object 
    let user = {
        name : "Stackoverflow Developer",
        age : 23
    };

    // appending properties to the user object
    user.location = "NYC";
    user["occupation"] = "JavaScript Developer";
    user["salary"] = 10;

    //(**)
    let isBreadwinner = () => {
        (user.salary) ? true : false;
    };

    console.log("asd" + isBreadwinner());
    user["Earning member"] = isBreadwinner();

    // printing the object
    console.dir("User dir object" + JSON.stringify(user));
    console.dir(user); //Earning member:undefined
</script>

I am trying to wrap my head around the isBreadwinner function. Why does not true pass into the (**) function. Why is Earning member:undefined

<script>
    "use strict"

    //creating a user object 
    let user = {
        name : "Stackoverflow Developer",
        age : 23
    };

    // appending properties to the user object
    user.location = "NYC";
    user["occupation"] = "JavaScript Developer";
    user["salary"] = 10;

    //(**)
    let isBreadwinner = () => {
        (user.salary) ? true : false;
    };

    console.log("asd" + isBreadwinner());
    user["Earning member"] = isBreadwinner();

    // printing the object
    console.dir("User dir object" + JSON.stringify(user));
    console.dir(user); //Earning member:undefined
</script>
Share Improve this question edited Aug 6, 2017 at 5:22 lft93ryt asked Aug 6, 2017 at 5:13 lft93rytlft93ryt 1,0161 gold badge17 silver badges34 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

You're missing the return in your isBreadWinner function.

let isBreadwinner = function() {
    return (user.salary) ? true : false;
};

If you want to skip using a return then you might want to use an ES6 arrow function which has implicit returns if everything is declared on one line without a block, {}.

let isBreadwinner = () => (user.salary) ? true : false;

you may write the function like this

let isBreadwinner = () => (user.salary) ? true : false;

or just add return to your function

Post a comment

comment list (0)

  1. No comments so far