未加星标

Goto Statement in JS

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二05 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏

Goto Statement in JS

If you are familiar with core javascript then you must know that there is no keyword like goto in JavaScript. So in this JavaScript tutorial, I will show you how to achieve the same thing you can get with the goto statement .

In other programming languages, you have seen that there is a goto statement. But in JavaScript, we are not introduced to anything like that.

Another alternative way to achieve the same is to use the tail calls. But unfortunately, we don’t have anything like that in JavaScript.

So generally, the goto is accomplished in JS using the below two keywords.

break continue How to use goto statement in JavaScript

Let’s take the below example,

var number = 0; Start_Position document.write("Anything you want to print"); number++; if(number < 100) goto start_position;

This is not a code. Just an example where you want to use goto statement.

Now I will show you how to achieve this in JavaScript

var number = 0; start_position: while(true) { document.write("Anything you want to print"); number++; if(number < 100) continue start_position; break; }

So using continue and break you can achieve the same.

Now I am going to show you another easy example of how to use these break and continue in JavaScript

var i; for(i=1;i<=10;i++){ console.log(i); }

The output will be:

Break keyword in JavaScript

Now if we want to get out of the loop for a certain condition then we can use break keyword.

Take the above example and add break into it for a certain condition.

var i; for(i=1;i<=10;i++){ document.write(i); if(i===6){ break; } } document.write("<br>I am broken");

Now the output will be like this one:

123456 I am broken

Here we just used the break keyword to get out of the loop.

Guess The Number Game Using Java with Source Code

3D Photo/Image Gallery (on space) Using html5 CSS JS

Understand Continue Keyword in JavaScript

Now again take the above example and add continue statement.

var i; for(i=1;i<=10;i++){ if (i===4 || i===2) { continue; } document.write(i); if(i===6){ break; } } document.write("<br>I am broken");

Now the output will be like this one:

1356 I am broken

Here the continue keyword is used to skip rest of the code inside the loop.

So the browser will skip printing the value of variable i, if the value is equal to 4 and 2.

You may also read,

Evolution of JavaScript JavaScript is gaining popularity Day by Day

Difference between JavaScript function in href and onclick

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

代码区博客精选文章
分页:12
转载请注明
本文标题:Goto Statement in JS
本站链接:https://www.codesec.net/view/611330.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(59)