JavaScript Functions: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 27: Line 27:
undefined
undefined
</syntaxhighlight>
</syntaxhighlight>
=Default Paramenter Value=
==Default Paramenter Value==
ECMAScript 2015 allows default parameter values in the function declaration:
ECMAScript 2015 allows default parameter values in the function declaration:
<syntaxhighlight lang='javascript'>
<syntaxhighlight lang='javascript'>

Revision as of 02:04, 10 December 2020

Internal

Overview

function name(parameter1, parameter2, parameter3) {
  // code to be executed
}

Function Parameters and Arguments

The names listed in the function definition are the function parameters. The values passed to the function are the function arguments.

A JavaScript function does not specify data types for its parameters, it does not perform type checking on its arguments and it does not check the number of arguments received.

Missing Argument

If a function is called with missing arguments, the corresponding argument values are set to undefined.

function test(a, b) {
    console.log(a);
    console.log(b);
}
test('something');
something
undefined

Default Paramenter Value

ECMAScript 2015 allows default parameter values in the function declaration:

function test(a, b = 2) {
  // ...
}

Declaration

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function

Function Expressions

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/function