JavaScript – Duplicate parameters in JavaScript Functions

Hi Devs,

In this article i will introduce you to one of the most confusing and common doubt for every newbie devs, Duplicate parameters in javascript functions

Table Of Contents

duplicating parameter in regular JS function
duplicating par…


This content originally appeared on DEV Community and was authored by capscode

Hi Devs,

In this article i will introduce you to one of the most confusing and common doubt for every newbie devs, Duplicate parameters in javascript functions

Table Of Contents

First we will see duplicating parameters in regular JavaScript function.

//this is syntax of duplicating parameter in js function
function Func (first, second, first){
        console.log(first, second, first);
    }

In non-strict mode, regular JavaScript functions allow duplicate named parameters

    function Func (first, second, first){
        console.log(first, second, first);
    }
     // first => 1  
     // second => 2
     // first => 3
    Func(1, 2, 3); // 3 2 3

    // first => 1
    // second => 2 
    // first => undefined
    Func(1,2); //undefined  [undefined, 2, undefined]

Lets check this in strict mode,

    function Func(first, second, first){
        "use strict"; 
        console.log(first, second, first);  
    }
    //Throws an error because of duplicate parameters (Strict mode)

In Strict mode we cannot duplicate the parameter name.

How do arrow functions treat duplicate parameters?

Now here is something about arrow functions:

Unlike regular functions, arrow functions do not allow duplicate parameters, whether in strict or non-strict mode. Duplicate parameters will cause a Syntax Error to be thrown._

    // Always throws a syntax error
    const Func =  (first, second, first)  =>                         
    {
        console.log(first, second);  
    }

CONGRATULATOIONS, YOU HAVE LEARNET ONE NEW TOPIC TODAY.
VISIT https://www.capscode.in/#/blog TO LEARN MORE...


This content originally appeared on DEV Community and was authored by capscode


Print Share Comment Cite Upload Translate Updates
APA

capscode | Sciencx (2021-07-02T04:32:04+00:00) JavaScript – Duplicate parameters in JavaScript Functions. Retrieved from https://www.scien.cx/2021/07/02/javascript-duplicate-parameters-in-javascript-functions/

MLA
" » JavaScript – Duplicate parameters in JavaScript Functions." capscode | Sciencx - Friday July 2, 2021, https://www.scien.cx/2021/07/02/javascript-duplicate-parameters-in-javascript-functions/
HARVARD
capscode | Sciencx Friday July 2, 2021 » JavaScript – Duplicate parameters in JavaScript Functions., viewed ,<https://www.scien.cx/2021/07/02/javascript-duplicate-parameters-in-javascript-functions/>
VANCOUVER
capscode | Sciencx - » JavaScript – Duplicate parameters in JavaScript Functions. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/07/02/javascript-duplicate-parameters-in-javascript-functions/
CHICAGO
" » JavaScript – Duplicate parameters in JavaScript Functions." capscode | Sciencx - Accessed . https://www.scien.cx/2021/07/02/javascript-duplicate-parameters-in-javascript-functions/
IEEE
" » JavaScript – Duplicate parameters in JavaScript Functions." capscode | Sciencx [Online]. Available: https://www.scien.cx/2021/07/02/javascript-duplicate-parameters-in-javascript-functions/. [Accessed: ]
rf:citation
» JavaScript – Duplicate parameters in JavaScript Functions | capscode | Sciencx | https://www.scien.cx/2021/07/02/javascript-duplicate-parameters-in-javascript-functions/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.