Home  >  Q&A  >  body text

javascript - How to make if and else execute and output console.log() at the same time

if(条件){
        console.log('ab')
    }else{
        console.log('cd')
    }
    
    条件这里可以添加任意代码
    最终结果是console.log('abcd')
    办法貌似有很多,但是好像很多人想到的是不能同时成立
黄舟黄舟2662 days ago985

reply all(8)I'll reply

  • 漂亮男人

    漂亮男人2017-07-05 10:49:34

    if (条件) {
        console.log('ab')
    } else {
        console.log('cd')
    }

    You can add any code here for the conditions
    The final result is console.log('abcd')
    There seem to be many ways, but it seems that many people think that it cannot be established at the same time


    Isn’t this a question from @南小鸟’s blog last time I read it? .


    01 Replacement

    if (console.log('abcd'), console.log = () => {}){
        console.log('ab');
    } else {
        console.log('cd'); 
    }

    02 Replacement

    if (console._log = console.log, console.log = str => console._log(str + 'cd')){
        console.log('ab');
    } else {
        console.log('cd'); 
    }

    03 bind

    if (!( console.log = console.log.bind(console, 'ab'))){
        console.log('ab');
    } else {
        console.log('cd'); 
    }

    But the result is that 'ab cd' has an extra space...

    04 setTimeout + replacement

    if (setTimeout(console.log.bind(null, 'abcd')), console.log = _ => _) {
        console.log('ab')
    } else {
        console.log('cd')
    }

    05 setTimeout + clear screen

    if (setTimeout(_ => { console.clear(), console.log('abcd') })){
        console.log('ab')
    } else {
        console.log('cd')
    }

    reply
    0
  • 女神的闺蜜爱上我

    女神的闺蜜爱上我2017-07-05 10:49:34

    . . . . . . . Is it because I don’t understand your needs or you lack basic programming logic
    if else is mutually exclusive

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-07-05 10:49:34

    ab abcd || cd abcd logically does not exist at the same time

    reply
    0
  • 扔个三星炸死你

    扔个三星炸死你2017-07-05 10:49:34

    if(condition){
    console.log('ab');
    }

    if (another condition) {
    console.log('cd');
    }

    if and else can only run one of the conditions. If the if condition is met, else will not be run

    reply
    0
  • 某草草

    某草草2017-07-05 10:49:34

    if(true){}else if(false){
       console.log('ab')
    }else{
       console.log('cd')
    }
    console.log('abcd')

    It’s just mind-boggling. .

    reply
    0
  • 高洛峰

    高洛峰2017-07-05 10:49:34

    if(console.log('ab')){
        console.log('ab'))
    }else{
        console.log('cd')
    }

    reply
    0
  • 漂亮男人

    漂亮男人2017-07-05 10:49:34

    I have a problem in my head. Which one should be written in the production environment and tried and executed at the same time? .

    reply
    0
  • 世界只因有你

    世界只因有你2017-07-05 10:49:34

    Looking at the adopted answers, the first question is about simultaneous execution. They are all about saving the country through curves just to output abcd. Where can I see that they are executed at the same time?

    reply
    0
  • Cancelreply