Best Way for Conditional Variable Assignment

后端 未结 12 1108
一个人的身影
一个人的身影 2021-01-31 02:26

Which is the better way for conditional variable assignment?

1st method

 if (true) {
   var myVariable = \'True\';
 } else {
   var myVariable = \'False\         


        
相关标签:
12条回答
  • 2021-01-31 02:54

    You could do a ternary, which is a lot shorter (and no darn curly braces):

    var myVariable = (true) ? 'True' : 'False';
    
    0 讨论(0)
  • 2021-01-31 02:56

    Third way when you are storing only true false in variabel then use

     var myVariable =(condition_written_in_if);
    
    0 讨论(0)
  • 2021-01-31 02:58

    Just for completion, there is another way in addition to all the others mentioned here, which is to use a lookup table.

    Say you have many possible values, you could declaratively configure a Map instead of using an if, switch or ternary statement.

    Object map = {
       key1: 'value1',
       key2: 'value2,
       keyX: 'valueX'
    };
    
    var myVariable = map[myInput];
    

    This works even for booleans:

    Object map = { true: 'value1', false: 'value2 };
    
    var myVariable = map[myBoolean];
    

    For booleans you would probably do it the 'normal' way though with logic operators specifically designed for that. Though sometimes it can be useful, such as:

    • portability: you can pass a map around
    • configurability: maybe the values come from a property file
    • readability: if you don't care it's a boolean or not, you just want to avoid conditional logic and reduce cognitive load that way

    Note there is some overlap between the advantages using a lookup map and advantages of using a function variable (closure).

    0 讨论(0)
  • 2021-01-31 02:58

    An alternative way of doing this is by leveraging the ability of logical operators to return a value.

    let isAnimal = false;
    let isPlant = true;
    
    let thing = isAnimal && 'animal' || isPlant && 'plant' || 'something else';
    
    console.log(thing);
    

    In the code above when one of the flags is true isAnimal or isPlant, the string next to it is returned. This is because both && and || result in the value of one of their operands:

    • A && B returns the value A if A can be coerced into false; otherwise, it returns B.
    • A || B returns the value A if A can be coerced into true; otherwise, it returns B.

    Answer inspired by this article: https://mariusschulz.com/blog/the-and-and-or-operators-in-javascript

    PS: Should be used for learning purposes only. Don't make life harder for you and your coworkers by using this method in your production code.

    0 讨论(0)
  • 2021-01-31 03:00

    try this

    var myVariable = (true condition) ? "true" : "false"
    
    0 讨论(0)
  • 2021-01-31 03:02

    It depends on the use for me. If I have code that I only want to run if true, but with no extra code for false, I'll use the second. If I want to execute some code on true, and different on false, I use the first. It all depends on use, but the general rule for me is to write once. Keep it clean, keep it simple, and keep it short

    0 讨论(0)
提交回复
热议问题