What happens if I don't pass a parameter in a Javascript function?

后端 未结 8 2121
余生分开走
余生分开走 2020-11-29 18:19

I am new to the world of Javascript and am tinkering with writing very basic functions and stumbled upon the example below by accident and am unsure why it works when I am n

相关标签:
8条回答
  • 2020-11-29 18:43

    All arguments in JavaScript functions are optional (read "loosely typed").

    JavaScript functions can be invoked with any number of arguments, regardless of the number of arguments named in the function definition. Because a function is loosely typed, there is no way for it to declare the type of arguments it expects, and it is legal to pass values of any type to any function. When a function is invoked with fewer arguments than are declared, the additional arguments have the undefined value.

    You can refer to a function's arguments within the function by using the named argument variables or the arguments object. This object contains an entry for each argument passed to the function, the first entry's index starting at 0. For example, if a function is passed three arguments, you can refer to the argument as follows:

    arguments[0]
    arguments[1]
    arguments[2]
    
    • JavaScript - The Definitive Guide, 5th Edition
    0 讨论(0)
  • 2020-11-29 18:43

    If you omit the argument, its value will be undefined. This enables you to create optional parameters quite easily.

    Another feature is the ability to define a function with no parameters, and call it with arguments successfully, making use of the arguments object. This lets you easily create variable-length argument arrays.

    0 讨论(0)
  • 2020-11-29 18:44

    In javascript console.log inside a function gives undefined as output for unpassed parameters but outside the function it gives not defined error as inside a function browsers explicitly declares the variable. For e.g

    console.log(x) 
    

    gives VM1533:1 Uncaught ReferenceError: x is not defined whereas

    function test(x) {
    console.log(x)
    }
    test(); 
    

    gives undefined. It is because the function test() is rewritten by browser as:

    function test(x) {
        var x;
        console.log(x)
        }
    

    Another Example : -

    var x =5 ;
    function test(x) {
    console.log(x)
    }
    test(); 
    

    is still undefined as the function becomes

    function test(x) {
        var x;
        console.log(x)
        }
    

    The alert in the below example will give undefined :-

        var x =5;
        function test() {
        alert(x);
        var x =10;
        }
    
    test(); 
    

    The above function will become :-

     function test() {
        var x;
        alert(x);
        x =10;
        }
    

    The scope of javascript variable inside a function is function level scope and not block level. For e.g.

    function varScope() {
    
    for(var i = 0; i < 10; i++){
        for(var j = 0; j < 5; j++){}
            console.log("j is "+j)
        }
        console.log("i is "+i);
    
    
    }
    varScope();
    

    will give output as :

    j is 5 
    i is 10
    

    Again the function has become as :-

      function varScope() {
        var i;
        var j;
        for(i = 0; i < 10; i++){
            for(j = 0; j < 5; j++){}
                console.log("j is "+j)
            }
            console.log("i is "+i);
        }
    
    0 讨论(0)
  • 2020-11-29 18:45

    That's just how JavaScript works. Parameters are optional, and will have the not-really-a-value value "undefined" in the function if they're missing from a function call.

    By "optional" I mean just that: invoking any function involves an arbitrarily long list of parameters. There need be no relationship between the number of parameters passed to a function and the number declared. Best way to think of this declaration, then:

    function x(a, b, c) {
      // ...
    }
    

    is that you're declaring a function and binding the name "a" to the first parameter, "b" to the second, and "c" to the third. It's by no means guaranteed, however, that any of those will actually be bound to a value in any given invocation of the function later.

    By the same token, you can define a function without any parameters at all, and then "find" them via the arguments object:

    function noArgs() {
      var a = arguments[0], b = arguments[1], c = arguments[2];
      // ...
    }
    

    So that's not quite the same as the first function, but it's close in most ways that count practically.

    The "undefined" value in JavaScript is a value, but it's semantics are kind-of unusual as languages go. In particular it's not exactly the same as the null value. Also, "undefined" itself is not a keyword; it's just a semi-special variable name!

    0 讨论(0)
  • 2020-11-29 18:48

    You can also provide more arguments than just the one mentioned in the function

    myFunction(1,2,3,4,5,6,7,'etc');
    

    You can use the arguments property which is an array in order to view the provided arguments.

    0 讨论(0)
  • 2020-11-29 18:49

    JavaScript doesn't have default values for function parameters like other languages do. So, you can pass as many or as little arguments as you want.

    If you don't pass a value, the parameter is undefined.

    function myfunction(x) {
        alert(x);
    }
    
    myfunction(); // alerts undefined
    myfunction(1); // alerts 1
    myfunction(1,2,3); // alerts 1
    

    If you pass more parameters than are in the signature, you can use arguments.

    function myfunction(x) {
        alert(x);
        console.log(arguments);
    }
    
    myfunction(1,2,3); // alerts 1, logs [1,2,3]
    
    0 讨论(0)
提交回复
热议问题