这个if语句能写成switch语句吗?

var a;
if(a===2){
   document.write('number 2');
}else if(a==='2'){
   document.write('string');
}else{
  document.write('others');
}

好像switch语句不能用来判断严格等于(===)?

qq_杀意隆_0
浏览 1820回答 3
3回答

woshiajuana

switch (1) {     case '1':         console.log(1);         break;     case 1:         console.log(2);         break; } //结果打印的是2,所以语句用的是严格等于===所以可以改写!谷歌浏览器F12在控制台复制代码按回车自己能动手的试试事情,偏偏要别人来告诉你一个结果

慕的地8582982

VSFQGWPIVOYQNWGGJNKEUIIKLBAKAWTGREKDXBXDDCZEWSYRLBEEGDNXCMKHAEBKRLSORHUPEVAHUWIIIUBHXQREGGGYITJZLIMNMKTEOYBBKENJYIIIKDXKHWSCFYIHEDACWVLVJSMJWJBOQZVFBXAJAQHJMXZPSIEHXPRODGKNQZJSENWMBZPFUZVAJSOKILODNXAJMJMDAPMPSLBEVLBCTCEPDYCSHYAWSWTCEVROKTHJTDYTQAKAQDTQTRAJZWNPZJUQYAJFWLVZVFBYCRAXNJZVFWLBQUYBWGCZOWMURBRUXMPZZPSDNQGJZWFNCGJLOXMVRJMHXZWZJMIXBMVEADMCSNKALADJUUTXCNWBDMCSCSBEMPJSHEAXBXFCSAPPMJSPFNQTPYHLNQMPLUKQGOEBRNDXAXADFTXADZBYVSIRHQNDZBEPNJLUENPSIXBNPJNQGCAPTCTWYVYUDTJTVBXNKBMIEADREHOEARAQMWGPKTPZVKAWMIIRTDTENEZPFMBQVKOLVQNJVLOPROENWZRZOSBZCFUKAQDZBMOSOYUEHQSOEVFFILHKSNDUQAVYOLUTDACMPMORURRAXUXSBYURUMLUFURAKSIFVQGVQVYOQURKTVRUQTWRBYBJMOLBEGBRVSBXTCRGWFVLFIMILVLPRHQIXNJGJAPKNKXZNDGWNJMILGOYURUEOXMCRTCEHKUXNDBFORHQADHQSVBXMVGPTKUKGLAWMBLTWLILWKHFHEAJMCHCRVYADNPZIYNKZVLNVSBXADLBSVXBKZJMWSBZWZURTYHTORNJYAQGWGEHDHJKGRGJGQRNQBKUDTPZPLOLVLIRSCSEACSIEZIROYLAWYBEAWMIMCXGXUEBFUEO
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript