![]() So these are two examples that may answer the queries if occurring that what if we use these values in the function call. Both null and empty strings are considered valid values for the function. Hence, it clearly means that if we pass either an empty string or null as the parameter value, the default will not be considered, and if an empty string is passed, the output will be an empty string, and if null is passed, the output will be null and not the default value. In the last function invocation, we have supplied null as its value which means it will consider the null value as its value and will console log the value of a as null only.In the third calling of the function, we have passed a (' '), which means it will console log the string value of the parameter. ![]() In the second calling of the function, we have passed undefined as the parameter value, which means the function will console log the default value here also.In the first invocation of the function, we have called the function with no passing of the value, so it will console log out the default value only.We have created a function where we have provided a default value to the function's parameter, and in the calling of the function, we have invoked the function four times.Let's see some more examples to better understand the concept:Įxample 1: Passing undefined as the parameterĮxample2() // outputs number and The value of a is: 5Įxample2(undefined) // output gives as 1Įxample2('') // outputs string and the value of a is:Įxample2(null) // outputs object and the value of a: null Few examples of JavaScript Default Parameters Let's see some more examples to understand the working of the default parameters more efficiently. As a result, you might now be able to understand the working and use of the default parameters.For the last invoking of the function, we have passed two values for both the parameters, and so the default values will not be passed for any of the parameters because values are being supplied.Next, we have invoked the function with only one value which means the second parameter has been supplied with a value, but the first parameter is still undefined, and hence the default value will be provided for the first parameter.Thus, when we have consle.log the invoking of function with no passing of values, the default values of both the parameters will get passed, and hence the default parameter comes into play.It can be seen that we have initialized both parameters with two default values. We have created a function sum (), where we have performed a simple addition of two values.It allows the named parameters to get initialized with a default value when either undefined or no value is passed.Ĭonsole.log(" Sum of the numbers is : " + sum()) // there is no argumentĬonsole.log(" Sum of the number is : " + sum(3)) // it has one argumentĬonsole.log(" Sum is : " + sum(7,3)) // have both argument When no value is passed in the function definition, a default value is being set for the function definition, known as the default parameter. In every programming language, both terms have the same meaning only. It means the parameters are initialized as the supplied arguments values. The argument values are the values that are passed in the function definition as parameter values. Function parameters are the values that are passed in the definition of the function, whereas function arguments are the real values that are passed in the function. The parameters and arguments in a function are both different. Many people confuse both terms, and so before explaining default parameters, we should be clear about these terms. Also, the prerequisite needed is one should know the difference between the terms parameter and argument as both these terms are different from one another. In this section, we will learn about JavaScript default parameters and look at some examples to understand the default parameters' usage. The same applies to JavaScript also where it enables the functions to have their default values. In terms of computer programming language, almost every programming language has one or the other specified default values for the in-built functions or properties. Similarly, a default value is a value that will always be available. The term default can be understood as an option that is always available when no other options are appropriate. gspeech_style_.Next → ← prev JavaScript Default Parameters Translation_audio_type = 'audiotaqi.uz/mpeg', Taqi.uz/taqi.uz/translation_audio_type = 'audiotaqi.uz/x-wav', Streamerphp_folder = 'http:taqi.uz/taqi.uz//includestaqi.uz/includestaqi.uz/', Copyright © 2018 - TAQI | Axborot texnologiyalari markazi
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |