Javascript Articles - Page 515 of 534

What is an alert box in JavaScript?

Krantik Chavan
Updated on 12-Jun-2020 13:58:13

1K+ Views

An alert dialog box is mostly used to give a warning message to the users. For example, if one input field requires to enter some text but the user does not provide any input, then as a part of validation, you can use an alert box to give a warning message.Nonetheless, an alert box can still be used for friendlier messages. Alert box gives only one button "OK" to select and proceed.ExampleYou can try to run the following code to learn how to add an alert box −Live Demo                                         Click the following button to see the result:                          

How to use window.location to redirect to a different URL with JavaScript?

Nishtha Thakur
Updated on 12-Jun-2020 13:43:23

555 Views

You might have encountered a situation where you clicked a URL to reach a page X but internally you were directed to another page Y. It happens due to page redirection.It is quite simple to do a page redirect using JavaScript on the client side. To redirect your site visitors to a new page, you just need to add a line in your head section as follows −ExampleYou can try to run the following code to learn how to use window.location to redirect to another URL. Here, we will redirect to the home pageLive Demo           ... Read More

What are label statements in JavaScript?

karthikeya Boyini
Updated on 08-Jan-2020 09:16:48

4K+ Views

JavaScript label statements are used to prefix a label to an identifier. A label can be used with break and continue statement to control the flow more precisely. A label is simply an identifier followed by a colon (:) that is applied to a statement or a block of code. We will see two different examples to understand how to use labels with break and continue.ExampleYou can try to run the following code to use labels to control the flow, with break statementLive Demo                    document.write("Entering the loop! ");   ... Read More

Why doesn't JavaScript have a goto statement?

Samual Sam
Updated on 12-Jun-2020 13:41:12

410 Views

JavaScript has a goto statement, which is a reserved keyword. It isn’t popular since it is not needed in JavaScript. Generally, it isn’t considered a good practice to use a goto statement.Using goto with JavaScript preprocessing is still considered good as shown below −var a = 0; [lbl] beginning: console.log("Demo Text!"); a++; if(i < 424) goto beginning;The above code gets translated like the following −var a = 0; beginning: while(true) {    console.log("Demo Text!");    a++;    if(i < 424) continue beginning;    break; }

How to use labels to control the Flow in JavaScript?

Priya Pallavi
Updated on 12-Jun-2020 13:40:11

490 Views

To control the flow in JavaScript, use labels. A label can be used with break and continue statement to control the flow more precisely. A label is simply an identifier followed by a colon (:) that is applied to a statement or a block of code. We will see two different examples to understand how to use labels with break and continue.ExampleYou can try to run the following code to use labels to control the flow, with break statement −Live Demo                                     ... Read More

How can I use goto statement in JavaScript?

Abhinanda Shri
Updated on 30-Jul-2019 22:30:21

3K+ Views

JavaScript goto statement is a reserved keyword. Generally, according to web standards it isn’t considered a good practice to use goto statement.Using goto with JavaScript preprocessing is still considered good as shown below.var a = 0; [lbl] beginning: console.log("Demo Text!"); a++; if(i < 424) goto beginning;The above code gets translated like the following −var a = 0; beginning: while(true) { console.log("Demo Text!"); a++; if(i < 424) continue beginning; break; }

How to break a loop in JavaScript?

Ankitha Reddy
Updated on 08-Jan-2020 08:31:10

446 Views

The break statement is used to break a loop and continue executing the code, which is after the loop.ExampleYou can try to run the following code to break a loop in JavaScriptLive Demo                          var text = "";          var i;          for (i = 0; i < 5; i++) {             if (i === 2) {                break;             }             text += "Value: " + i + "";          }          document.getElementById("test").innerHTML = text;           OutputValue: 0 Value: 1

What is the best way to break from nested loops in JavaScript?

Jennifer Nicholas
Updated on 12-Jun-2020 13:16:07

1K+ Views

The best way to break from nested loops is to use labels. A label can be used with break and continue to control the flow more precisely. A label is simply an identifier followed by a colon (:) that is applied to a statement or a block of code.ExampleYou can try to run the following code to implement Label with a break statement to break from nested loops −Live Demo                    document.write("Entering the loop! ");          outerloop:  // This is the label name          for (var ... Read More

How to use for...in statement to loop through an Array in JavaScript?

Abhishek Kumar
Updated on 10-Nov-2022 08:50:04

285 Views

We use the for...in statement of JavaScript for looping over enumerable properties of an array or object. It is a variety of for loops. Unlike other loop constructs in JavaScript, the for...in loop doesn’t have to bother about the number of iterations. This is because the iteration size is fixed based on the enumerable properties of the object or the number of elements in the array. The "for...in" statement in JavaScript A for...in loop iterates over all the enumerable properties of an object. All the properties that are assigned using a simple assignment operator or by default initializer are considered ... Read More

What is the difference between break and continue statements in JavaScript?

Smita Kapse
Updated on 13-Jun-2020 13:22:14

676 Views

break statementThe break statement is used to exit a loop early, breaking out of the enclosing curly braces.  The break statement exits out of a loop.  Let’s see an example of break statement in JavaScript. The following example illustrates the use of a break statement with a while loop. Notice how the loop breaks out early once x reaches 5 and reaches to document.write (..) statement just below to the closing curly brace, Example                    var x = 1;          document.write("Entering the loop ");         ... Read More

Advertisements