Found 10878 Articles for Web Development

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

Arjun Thakur
Updated on 08-Jan-2020 06:46:47

7K+ 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 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));              

What does the operator || do in a var statement in JavaScript?

Shubham Vora
Updated on 31-Oct-2022 11:40:20

133 Views

In this tutorial, we will discuss what the operator || does in a var statement in JavaScript. The logical OR operator or logical disjunction on the operands returns true if any of the operand values are true, false, ’’, null, undefined, 0, and NaN are the false values. All other values are true. The logical OR operands must be either boolean, integer, or a pointer kind. The logical OR returns the latest operand if there is no true value. The logical OR executes from left to right. Users can follow the syntax below for using the logical OR operator. Syntax ... Read More

What is the purpose of a self-executing function in JavaScript?

Fendadis John
Updated on 12-Jun-2020 12:08:10

320 Views

The purpose of a self-executing is that those variables declared in the self-executing function are only available inside the self-executing function.Variables declared in the self-executing function are, by default, only available to code within the self-executing function.It is an immediately invoked function expression (IIFE). It is a function, which executes on creation.SyntaxHere is the syntax −(function() {    // code })();As you can see above, the following pair of parentheses converts the code inside the parentheses into an expression:function(){...}In addition, the next pair, i.e. the second pair of parentheses continues the operation. It calls the function, which resulted from the ... Read More

How to extract the hostname portion of a URL in JavaScript?

Shubham Vora
Updated on 23-Aug-2022 09:20:54

9K+ Views

In this tutorial, we will see how to extract the hostname portion of a URL in JavaScript. What is a URL? An alternative term for a web address is a URL. For example, tutorialpoints.com is a word-based URL. An IP address can also be used as a URL (ex. 192.168.2.24). Since names are simpler to recall than numbers, most users submit the name’s address when searching on the internet A URL is a method by which web browsers ask web servers for specific pages. The syntax/format of a URL is given below. Syntax scheme://prefix.domain:port/path/filename Parameters scheme − specifies ... Read More

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

George John
Updated on 08-Jan-2020 06:30:25

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

What is a standard for commenting a function in JavaScript?

Saurabh Jaiswal
Updated on 05-Jan-2023 16:15:47

6K+ Views

JavaScript is used everywhere, from creating the back end using environments like Node.js to creating the front end using React.js, Vue.js, etc. In JavaScript, the uses of a function are very high, it is used for performing a group operation, it is used as a callback, as a constructor, and in many more places. With the high use of functions in JavaScript, the code becomes full of functions everywhere and looks messy and hard to debug. At a point, it is difficult to find which functions trigger which event and which callbacks are used for what, so it is ... Read More

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

Paul Richard
Updated on 08-Jan-2020 06:27:14

2K+ 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 iframeLive Demo                    function display(){             alert("Hello World!");          }                      Click          

How to use unlimited arguments in a JavaScript function?

Shubham Vora
Updated on 31-Oct-2022 10:58:12

2K+ Views

In this tutorial, let us discuss the methods to use unlimited arguments in a JavaScript function. The number of arguments in a JavaScript function is a bit tricky. If we specify three arguments should follow the correct order of the arguments. Let us discover the solution to this problem by introducing the methods to pass unlimited in a function, we can not pass more than three. Moreover, we arguments to a JavaScript function. Using ES5 Arguments Object Instead of converting the arguments object to an array, we can process the arguments object with the total number of arguments. Follow the ... Read More

How can I declare optional function parameters in JavaScript?

Shubham Vora
Updated on 12-Jul-2022 14:00:36

9K+ Views

This tutorial will teach us how to declare optional function parameters in JavaScript. While declaring the function, we pass some variables to the function definition to use it inside the function block, called function parameters. The function parameters can also be optional, which gives us the independence to pass function arguments when we call the function. Here, Arguments are the values we pass while calling the function, and parameters are the variables we pass in the function definition.Optional ParametersThe optional parameter word means that you don’t need to pass that parameter every time you call the function, giving you the ... Read More

What is Multiplication Assignment Operator (*=) in JavaScript?

Abhishek
Updated on 08-Nov-2022 06:26:51

369 Views

In this article, we will learn about the Multiplication assignment operator (*=) in JavaScript. In general, we use a = a * b this expression to update the value of a variable with the value returned after multiplying its original value with some other value, which is easy to understand, but it time taking to write again and again if we are using updating values of more variables like this. So, to overcome this problem, JavaScript introduces us to the multiplication assignment operator (*=) as it is the short form for using the above expression. The multiplication Assignment operator (*=) ... Read More

Advertisements