Conditional Statements (if...if else...switch)

1- if (MDN)

const myName = "javascript";

if(myName == "javascript"){                          //when i see double == then I convert variable to string first then compare
     console.log('My name is javascript');       //this will print
}

if(myName === "js"){                          //when i see triple === then I compare without converting variable to string
     console.log('My name is js');       //this will not print as myName is equals to 'javascript'
}

 

 

2- if else(MDN)

const myName = "javascript";

if(myName == "js"){                         
     console.log('My name is javascript');       //this won't be printed
}

else {                          
     console.log('My name is javascript');       //this will be printed 
}

 

 

 

3- switch (MDN)

const myName = 'javascript';
switch (myName) {
  case 'javascript':                //this will work
    console.log('Javascript');
    break;
  case 'js': 
    console.log('js');
    break;
  default:
    console.log(`I don't know my name`);
}

 

 

Try it all on JSBin. 🤓

Discussion

Be the first one to comment!