Introduction To JavaScript Functions
Published: 2024-02-05 02:54:26

## Basics Of A Function

I have been introduced into the programming language of JavaScript. I have obtained a vast amount of information of the core fundamentals about the language, such as functions and data types. Upon the first day of being introduced, I had a decent amount of background information about the language. First thing I was taught was functions and its importance. First things first is how to structure a function properly. After typing "function" and the name of it following it, you must include "()" after it. Following after the parenthesis, you use "{}" which is where your argument, or executable code, will be. There are other types of a function, which will be later talked about below.

## JavaScript Variables
In Javascript, variables are used to store values for future use, such as within function arguments. There are 3 different types variable declarations that we have when assigning it to a variable. Along with creating a function, there will come along something called "parameters". Think of parameters as a value place holder that will later be fulfilled later on. Parameters are placed in parenthesis after the function name, if there are any, if not, leave an empty parenthesis. The three are const, let, and var. 'Const' will be primarily used when the value assigned to the variable will be a static and will not be reassigned or redeclared further on. 'Let' will be primarily used for when the variable will have a chance to be reassigned, but the value CANNOT be redeclared. For 'var', due to its outdated architecture, it has been superseded as a variable declaration by 'let' and 'const'.

Example: `function add (variable1, variable2) {
let sum = variable1 + variable2
return sum; 
} sum(10,5) //Output: 15`

##Arrow Functions
As previously stated above, there are multiple types of functions. In this section, I will elaborate on arrow functions and its application. Arrow functions have a different format than traditional functions. 

`const multiply = (parameter1, parameter2) => parameter1 * parameter2;
multiply(5,5); //Output: 25`

As shown above, you can see that there are no curly braces to put your argument in. So where does your argument expression go? It goes after the "=>" which is basically a replacement for the curly braces. To structure an arrow function from scratch you will have to create a variable and assign it to the argument. For the parameters within the parenthesis, that will be your value placeholders for that function. 

## Anonymous Functions
Anonymous functions are a rather simple concept, they're basically a regular function, but without a name. An important note, is that you'll compile a syntax error if the anonymous function is not within a parenthesis. An anonymous function will need to be assigned to a variable for it to be further accessible (invoked). The following example will show an anonymous function that'll display a message:

`let message = function() {return 'This is an anonymous function'}

message()`
//=> Output: This is an anonymous function

As shown above the function has no name between the keyword "function" and the parenthesis, hence the name anonymous function. Due to the fact that we need to call the function later in the code, we assign the function to the 'message' variabl.

## CallBack Functions
Functions can pass numbers, strings, objects, and arrays, but we can also pass functions into other functions. Functions are objects and this is why we are allowed to achieve this. One thing you may have noticed is the function that is getting called-back is an anonymous function. It doesn't have to be an anonymous function for the code to execute, you can name it if preferred.

Example:
`function returnFunction(parameter) {
return parameter;
}
->
function returnFunction(function() {return 2 + 2}
//=> returns parameter, in this case the function set as the parameter. Output: function() {return 2 + 2}`

## Conclusion

As I further progress into the world of JavaScript, I always find myself always reflecting back on functions and their proper application within the language. Majority of my labs and lectures in JavaScript, will always use some form of a function. Each function has their own unique characteristics and specific applications to where they are the most optimal approach on tackle lines of code. This may seem like a lot of information straight-forward, but it is completely okay if you do not obtain every piece upon a single read. Coding takes hours and hours of practice, and endless cases of trial and error. Take it piece by piece until moving forward to the next subject as it can get very confusing very fast. On that note, this wraps up my blog on JavaScript functions and their importances and applications within the language.