dimanche 2 juin 2019

How to code if-statement with short-circuit

first of all, I am new to javascript and just finished all the basic stuff about JS. And I have run into short-circuiting, made me try to code if statement with it.

the code below, the first one is trying to see if the input is 1, show on a console page, 'yes', otherwise, 'no'. However, for some reason, i could see both, 'yes' and 'no', when input is 1.

then, i figured it out that, console.log('yes') is also giving 'undefined'.

how can I fix this?

and if i want to replace console.log() with return, how can i do that? the second code with return is producing syntax error.

thank you in advance

checking(1); 
function checking (input) {
    ((input==1 && console.log('yes')) || console.log('no'))
}
// output => yes 
// output => no

and with return

function checking (input) {
    ((input==1 && return 'yes') || return 'no')
}

Aucun commentaire:

Enregistrer un commentaire