JavaScript FunctionsJavaScript functions are used to perform operations. We can call JavaScript function many times to reuse the code. Advantage of JavaScript functionThere are mainly two advantages of JavaScript functions.
JavaScript Function SyntaxThe syntax of declaring function is given below. JavaScript Functions can have 0 or more arguments. JavaScript Function ExampleLet’s see the simple example of function in JavaScript that does not has arguments. Test it NowOutput of the above exampleJavaScript Function ArgumentsWe can call function by passing arguments. Let’s see the example of function that has one argument. Test it NowOutput of the above exampleFunction with Return ValueWe can call function that returns a value and use it in our program. Let’s see the example of function that returns value. Test it NowOutput of the above exampleJavaScript Function ObjectIn JavaScript, the purpose of Function constructor is to create a new Function object. It executes the code globally. However, if we call the constructor directly, a function is created dynamically but in an unsecured way. SyntaxParameterarg1, arg2, .... , argn - It represents the argument used by function. functionBody - It represents the function definition. JavaScript Function MethodsLet's see function methods with description.
JavaScript Function Object ExamplesExample 1Let's see an example to display the sum of given numbers. Test it NowOutput: 7 Example 2Let's see an example to display the power of provided value. Test it NowOutput: 8
Next TopicJavascript Objects
|