Javascript Articles

Page 8 of 534

How can I pass a parameter to a setTimeout() callback?

Daniol Thomas
Daniol Thomas
Updated on 11-Mar-2026 2K+ Views

To pass a parameter to setTimeout() callback, use the following syntax −setTimeout(functionname, milliseconds, arg1, arg2, arg3...)The following are the parameters −function name − The function name for the function to be executed.milliseconds − The number of milliseconds.arg1, arg2, arg3 − These are the arguments passed to the function.ExampleYou can try to run the following code to pass a parameter to a setTimeout() callback           Submit                function timeFunction() {             setTimeout(function(){ alert("After 5 seconds!"); }, 5000);          }          Click the above button and wait for 5 seconds.    

Read More

How to add a number of months to a date using JavaScript?

Paul Richard
Paul Richard
Updated on 11-Mar-2026 809 Views

To add a number of months to a date, first get the month using getMonth() method and add a number of months.ExampleYou can try to run the following code to add a number of months                    var d, e;          d = new Date();          document.write(d);          e = d.getMonth()+1;          document.write("Incremented month = "+e);          

Read More

How to call a parent window function from an iframe using JavaScript?

Paul Richard
Paul Richard
Updated on 11-Mar-2026 4K+ Views

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 iframe                    function display(){             alert("Hello World!");          }                      Click          

Read More

Is it required to have a return a value from a JavaScript function?

George John
George John
Updated on 11-Mar-2026 2K+ Views

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.                    function concatenate(first, last){          var full;          full = first + ...

Read More

How to write a JavaScript function to get the difference between two numbers?

Arjun Thakur
Arjun Thakur
Updated on 11-Mar-2026 9K+ Views

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 numbers                    var num1, num2;          num1 = 50;          num2 = 35;          var difference = function (num1, num2){             return Math.abs(num1 - num2);          }          document.write("Difference = "+difference(num1,num2));              

Read More

How to use JavaScript to hide a DIV when the user clicks outside of it?

Rishi Raj
Rishi Raj
Updated on 11-Mar-2026 1K+ Views

To hide a div when the user clicks outside of it, try to run the following codeExample                    window.onload = function(){             var hideMe = document.getElementById('hideMe');             document.onclick = function(e){                if(e.target.id !== 'hideMe'){                   hideMe.style.display = 'none';                }             };          };             Click outside this div and hide it.    

Read More

How to break a loop in JavaScript?

Ankitha Reddy
Ankitha Reddy
Updated on 11-Mar-2026 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 JavaScript                          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

Read More

What are label statements in JavaScript?

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 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 statement                    document.write("Entering the loop! ");     ...

Read More

What is onclick event in JavaScript?

vanithasree
vanithasree
Updated on 11-Mar-2026 682 Views

The onClick event is the most frequently used event type, which occurs when a user clicks the left button of the mouse.ExampleYou can put your validation, warning etc., against this event type.                                         Click the following button and see result                          

Read More

What is the difference between break with a label and without a label in JavaScript?

Samual Sam
Samual Sam
Updated on 11-Mar-2026 618 Views

Break without labelThe break statement is used to exit a loop early, breaking out of the enclosing curly braces.  The break statement exits out of a loop. ExampleLet’s see an example of break statement in JavaScript without using label −                            var x = 1;          document.write("Entering the loop ");                  while (x < 20) {             if (x == 5){                break; // breaks out of ...

Read More
Showing 71–80 of 5,338 articles
« Prev 1 6 7 8 9 10 534 Next »
Advertisements