Javascript Articles

Page 10 of 534

What is the use of JavaScript eval function?

Vrundesha Joshi
Vrundesha Joshi
Updated on 11-Mar-2026 400 Views

The JavaScript eval() is used to execute an argument. The code gets execute slower when the eval() method is used. It also has security implementations since it has a different scope of execution. In addition, use it to evaluate a string as a JavaScript expression.The eval() method is not suggested to use in JavaScript since it executes slower and improper usage opens your website to injection attacks.ExampleHere’s how you can implement eval() function                    var a = 30;          var b = 12;          var res1 = eval("a * b") + "";          var res2 = eval("5 + 10") + "";          document.write(res1);          document.write(res2);          

Read More

Which one is better to use for a JavaScript link, “#” or “javascript:void(0)”?

Smita Kapse
Smita Kapse
Updated on 11-Mar-2026 237 Views

Using “javascript:void(0)” is definitely better, since its faster. Try to run both the examples in Google Chrome with the developer tools. The “javascript:void(0)” method takes less time than the only #.Here’s the usage of “javascript: void(0)”:If inserting an expression into a web page results in an unwanted effect, then use JavaScript void to remove it. Adding “JavaScript:void(0)”, returns the undefined primitive value.The void operator is used to evaluate the given expression. After that, it returns undefined. It obtains the undefined primitive value, using void(0).The void(0) can be used with hyperlinks to obtain the undefined primitive valueExample       ...

Read More

How to determine if an argument is sent to the JavaScript function?

Krantik Chavan
Krantik Chavan
Updated on 11-Mar-2026 151 Views

To determine if an argument is sent to function, use “default” parameters.ExampleYou can try to run the following to implement it                    function display(arg1 = 'Tutorials', arg2 = 'Learning') {             document.write(arg1 + ' ' +arg2+"");          }          display('Tutorials', 'Learning');          display('Tutorials');          display();          

Read More

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 811 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 448 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
Showing 91–100 of 5,339 articles
« Prev 1 8 9 10 11 12 534 Next »
Advertisements