how to convert if else statement to ternary operators

匿名 (未验证) 提交于 2019-12-03 02:43:01

问题:

how can I rewrite the following javascript code to ES6 version:

if(allUser[currentUser]['email']==email){         if(allUser[currentUser]['password']==password){             isUserFound = true             passwordCorrect = true              break         } else {             isUserFound = true             passwordCorrect = false             break         }     } else {         isUserFound = false     } 

回答1:

That would be a bad idea, your code is more readable with if/else. You could simplify to:

isUserFound = allUser[currentUser]['email'] === email; passwordCorrect = isUserFound && allUser[currentUser]['password'] == password 

In case you didn't know, break also has no effect on an if, only for, while and switch.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!