admin管理员组文章数量:1287961
I have a plicated method with if else if else constructs in typescript. Its a void method. How do i break from the method on some particular if condition so that other conditions need not to be executed. This helps me in avoiding the else condition ;I can keep on writing if
In Java this can be achieved using return;
inside the void method.
I have a plicated method with if else if else constructs in typescript. Its a void method. How do i break from the method on some particular if condition so that other conditions need not to be executed. This helps me in avoiding the else condition ;I can keep on writing if
In Java this can be achieved using return;
inside the void method.
3 Answers
Reset to default 7This is the the same in Javascript and Typescript as in Java : you just use a simple return;
.
if (true) {
return;
}
console.log('Never printed')
It is also the same in several other languages I can think of, like C, C++, C#, and many others with some slight variations on the syntax.
You can use an empty return
to exit the function execution:
function test() {
if(true) {
return;
} else if (true) {
console.log('It will not be printed, because "return" is in first "if" statement');
}
}
test();
May be your value is returning an undefined type. In typescript I check value vs null, but only in some types it works, for other types like Date I have to check with the undefined possibility too, like this:
if (this.creationDate === null || this.creationDate === undefined) { return; }
本文标签: JavaScriptTypescript returnbreak from a void methodStack Overflow
版权声明:本文标题:JavaScriptTypescript returnbreak from a void method - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741331562a2372795.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论