it is a reusable block of code/programming statement to perform a certain task.
a function can return a value or not.
Declaration for a function:
function-key-word + function-name + “(parameter)”
# Why using a Function?
functions are:
clean & easy to read
reusable
easy to test
# Some Ways of Creating a Function
Declaration function
Expression function
Anonymous function
Arrow function
# Writing a Function
# Declaration Function
Note (for unlimited number of parameters):
a function declaration provides a function scoped arguments array like object. Anything we pass as argument in the function can be accessed from the arguments object inside the function.
# Anonymous Function
Anonymous function/without name
# Expression Function
these are anonymous functions.
after we create a function without a name and we assign it to a variable.
to return value from the function we should call the variable.
# Self Invoking Function
these are anonymous functions.
they do not need to be called to return a value.
# Arrow Function
is an alternative to write a function - but there are some minor differences.
arrow functions use => instead of keyword function to get declared.
after the declaration there is the (a, b, ...) parameterlist.
# Function with default value
sometimes we pass default values to parameters.
when the function gets invoked & we don’t pass an argument, the default value will be used.