Found 10711 Articles for Web Development

How to use Spread Syntax with arguments in JavaScript functions?

Abhishek Kumar
Updated on 10-Nov-2022 08:28:14

1K+ Views

We use the Spread Syntax of JavaScript to expand an array, string, or object in place. Such types of values are called iterable. This is similar to destructuring the iterable in place. Its utility in a function call allows us to extract function parameters from an iterable. In this tutorial, we learn how to use Spread Syntax with arguments in JavaScript functions. Spread operator in JavaScript A Spread operator, denoted with (...) followed by the name of the iterable expands the iterable into its constituent elements. e.g. const [x, y, ...z] = [1, 2, 3, 4, 5] This creates ... Read More

What is the difference between rest parameters and the arguments object in Javascript?

Manikanth Mani
Updated on 16-Jun-2020 08:03:52

609 Views

Rest parametersWith rest parameter, you can represent a number of arguments as an array. ES6 brought rest parameter to ease the work of developers. For arguments objects, rest parameters are indicated by three dots … and precedes a parameter.Arguments objectArguments object in JavaScript is an object, which represents the arguments to the function executing.Here is the difference between rest parameters and the arguments object.Arguments object includes all arguments passed to the function, whereas rest parameters are those, which are not given another name.The rest parameters are Array instances, whereas arguments object isn’t an array. Array instances are the following methods: ... Read More

What are async methods in JavaScript?

varun
Updated on 15-Jun-2020 13:18:08

117 Views

The async function declaration as the name suggests defines an asynchronous function. This function returns an AsyncFunction object.SyntaxHere’s the syntax −async function functionname([param[, param[, ... param]]]) {    statements to be executed }ExampleLet’ see an example, which prints the result after 5 seconds −                    function displayFunction(num) {             return new Promise(resolve => {                setTimeout(() => {                   resolve(num);                }, 5000);             });          }          async function add2(num) {             const x = displayFunction(7);             const y = displayFunction(5);             return num * await x * await y;          }          add2(15).then(result => {             document.write("Multiplication Result (after 5 seconds): "+result);          });          

What are generator functions in JavaScript?

Prabhas
Updated on 16-Jun-2020 06:27:28

193 Views

Generator Functions allows execution of code in between when a function is exited and resumed later. So, generators can be used to manage flow control in a code. Cancel asynchronous operations easily since execution can be paused anytime.Here’s the syntax; do not forget to add an asterisk after the “function” keyword. You can add an asterisk using any of the following −function *myFunction() {} // or function* myFunction() {} // or function*myFunction() {}ExampleLet’s see how to use a generator functionLive Demo                    function* display() {             ... Read More

How to define a method in JavaScript?

seetha
Updated on 07-Jan-2020 08:07:26

387 Views

A method in JavaScript is the action performed on objects. A JavaScript method has a function definition, which is stored as a property value. ExampleLet’s see an example to define a method in JavaScriptLive Demo                                 var department = {                deptName: "Marketing",                deptID : 101,                deptZone : "North",                details : function() {                   return "Department Details" + "Name: " + this.deptName + " Zone: " + this.deptZone + "ID: " + this.deptID;                }             };          document.getElementById("myDept").innerHTML = department.details();          

What is “Parameters without defaults after default parameters in JavaScript”

vanithasree
Updated on 16-Jun-2020 06:26:44

95 Views

The default parameter came to handle function parameters with ease. Default parameters allow you to initialize formal parameters with default values. This is possible only if no value or undefined is passed. With ES6, you can easily set default parameters. Let’s see an exampleExampleLive Demo                    // default is set to 1          function inc(val1, inc = 1) {             return val1 + inc;          }          document.write(inc(10, 10));          document.write("");       ... Read More

How to define functions inside a function body in JavaScript?

mkotla
Updated on 07-Jan-2020 08:06:24

118 Views

To achieve what you want, use JavaScript Closures. A closure is a function, which uses the scope in which it was declared when invoked. It is not the scope in which it was invoked.ExampleLet’s take your example and this is how you can achieve your task. Here, innerDisplay() is a JavaScript closure.Var myFunction = (function () {    function display() {       // 5    };    function innerDisplay (a) {       if (/* some condition */ ) {          // 1          // 2          display();       }else {          // 3          // 4          display();       }    }    return innerDisplay; })();

How to pass the value 'undefined' to a function with multiple parameters in JavaScript?

Shubham Vora
Updated on 20-Oct-2022 08:18:41

3K+ Views

In this tutorial, we will learn how to pass the value 'undefined' to a function with multiple parameters in JavaScript. In JavaScript, the data type of 'undefined' is primitive. When a variable is declared, JavaScript automatically assigns the value 'undefined' to it. If a function has multiple parameters and one of the parameters' values is not available at the moment, then we need to omit that parameter's value from the function call. But if we omit the parameter's value with a blank space, the JavaScript will show an error. Syntax function abc(param1, param2, param3) { console.log(param1, ... Read More

How to use a line break in array values in JavaScript?

Abhishek Kumar
Updated on 10-Nov-2022 08:33:01

12K+ Views

We use the join() method of JavaScript to use a line break in array values. It allows us to concatenate all the constituent elements of an array into a single string using a common separator. The join() method in JavaScript The join() method takes as input a single separator string and returns a string with all the elements of the array separated by the specified separator string. The separator string by default is the comma(, ). The join method uses the toString() method to convert the elements of the array into corresponding strings. A null or undefined value is converted ... Read More

What is lexical this in JavaScript?

Sreemaha
Updated on 16-Jun-2020 06:24:31

1K+ Views

Fat arrow function solves the issue of lexical binding “this”. It gets the context of “this “and you can fulfill the same purpose since fast arrow does not have its own this. Fat arrow function as the name suggests helps in decreasing line of code. The syntax => shows fat arrow.Example$('.button1').click(function () {    setTimeout(function () {       $(this).text('demo');    } ,400); });The above gives an error since the function() defines this as a global object. Let’s see how to solve it using fat arrow function and the context of “this” −$('.button1').click(function () {    setTimeout( () => ... Read More

Advertisements