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
Phan😉

Phan😉 4:14 PM, Tuesday 8th Dec, 2020

Thats all🤔

1

Naman Gupta

Naman Gupta 12:02 PM, Saturday 12th Dec, 2020

Hey. I have covered just the basics in this codelab. That was the aim. 😊