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
The purpose of a while loop is to execute a statement or code block repeatedly as long as an expression is true. Once the expression becomes false, the loop terminates.ExampleYou can try to run the following code to learn how to use nested while loopLive Demo var height = 2; var width = 8; var col = 0; var row = 0; document.write("Starting Loop "); while (row < height) { col = 0; while(col < width) { document.write("#"); col++; } document.write(""); row++; } document.write("Loop stopped!");
The break statement is used to exit a loop early, breaking out of the enclosing curly braces.ExampleYou can try to run the following code to learn how to use the break statement to come out of a loopLive Demo var x = 1; document.write("Entering the loop "); while (x < 20) { if (x == 5) { break; // breaks out of loop completely } x = x + 1; document.write( x + ""); } document.write("Exiting the loop! ");
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
The CSS background-position property is used to set an initial position for a background image relative to the origin.SyntaxThe syntax of CSS background-position property is as follows −Selector { background-position: /*value*/ }The following examples illustrate CSS background-position property −Example Live Demo div { margin: 30px; padding: 100px; background-image: url("https://www.tutorialspoint.com/images/C-programming.png"), url("https://www.tutorialspoint.com/images/Swift.png"), url("https://www.tutorialspoint.com/images/xamarian.png"); background-repeat: no-repeat; background-position: 15% 20%, top, right; border: 1px solid; } OutputThis gives the following output −Example Live Demo p { margin: 20px; padding: 80px; background-image: url("https://www.tutorialspoint.com/images/dbms.png"), url("https://www.tutorialspoint.com/images/Operating-System.png"); ... Read More
It performs XOR operation on the right operand with the left operand and assigns the result to the left operand.ExampleYou can try to run the following code to learn how to work with Bitwise XOR Assignment OperatorLive Demo var a = 2; // Bit presentation 10 var b = 3; // Bit presentation 11 document.write("(a ^= b) => "); document.write(a ^= b);
To redirect from an HTML page, use the META Tag. With this, use the http-equiv attribute to provide an HTTP header for the value of the content attribute. The value of the content is the number of seconds; you want the page to redirect after.Through this, you can automatically redirect your visitors to a new homepage. Set the content attribute to 0, if you want it to load immediately.ExampleThe following is an example of redirecting current page to another page after 2 seconds.Live Demo Redirection This page will redirect in 2 seconds.
Use Math.abs() inside a JavaScript function to get the difference between two numbers in JavaScript.ExampleYou can try to run the following code to get the difference of numbersLive Demo var num1, num2; num1 = 50; num2 = 35; var difference = function (num1, num2){ return Math.abs(num1 - num2); } document.write("Difference = "+difference(num1,num2));
A JavaScript function can have an optional return statement i.e. it’s optional to return a value. This statement should be the last statement in a function.For example, you can pass two numbers in a function and then you can expect the function to return their multiplication to your calling program.ExampleTry the following example. It defines a function that takes two parameters and concatenates them before returning the resultant in the calling program.Live Demo function concatenate(first, last){ var full; full = first ... Read More
To call a parent window function, use “window.top”.ExampleYou can try to run the following code to call a parent window function from an iframeLive Demo function display(){ alert("Hello World!"); } Click
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP