Complete Introduction to JavaScript Functions! βββ
π Follow Mega Thread π§΅
Content: 1. What is a function? 2. Different types of functions.
3 to 5. Functions Anatomy. 6. Difference in hoisting between function types. 7. Sources to learn other differences in function types. 8. Use cases for each function type.
1. What is a function
In simple words, a function is a piece of code that performs a specific task.
Such city (function) has its own citizens (variables) and city limits (function scope).
Function scope means that variables inside the function are only accessible within this function and you can't call them outside.
So, we're separating small functionalities into functions to reuse them in multiple places by calling them like this: myFunction()
2. Different types of functions
Generally, we have 3 types of functions:
1. Function Declaration 2. Function Expression with "function" keyword 3. Arrow Function Expression
Let's see how they look like π
3. Function Declaration Anatomy
4. Arrow Function Expression Anatomy
5. Function Expression with "function" keyword Anatomy
6. Difference in hoisting between function types
In the context of functions, Hoisting is a behaviour in JavaScript where Functions Declarations are moved to the top of the file scope.
So, it basically means that if you wrote your function as Function Declaration, you will have access to it everywhere inside your js file! However, it does not work in this way with Function Expressions.
You can call Function Declaration before declaring it, see an example. π
7. Sources to learn other differences in function types
I also suggest visit these resources to understand key differences between functions types better π
Article: dmitripavlutin.com/differences-beβ¦
Video:
8. Use cases for each function type.
It's choice of your preferences which type of function do you use more often.
However, I'm doing it in this way π
- I use the Function Declaration type for bigger functions that I want to reuse. In this case, you can always benefit from hoisting and calling it wherever you want.
- I Use Arrow Function Expression in smaller functions and inside of array methods. In this case, you can benefit from a small size and elegant look.
- I don't use Function Expression with "function" keyword because it's just an older way of writing Function Expressions and doesn't have benefits over the Arrow Functions.
That's all folks! π₯π§―
If you found this thread useful, please consider following @eugZolotarenko and retweeting the first tweet. π’
β’ β’ β’
Missing some Tweet in this thread? You can try to
force a refresh
It is an easy to use tool that allows you to extract, view and download images from any public website. Simply paste the URL of the website into the input field and click "Extract" to start the process.
5 ways to Make Money by building projects for your Web Developer Portfolio! π°π°π°
π Follow Thread π§΅
1. HTML Website Template
For creating HTML Template, you need to know only HTML, CSS, and a little bit of JavaScript. However, it can easily help you stand out from the crowd if you include such a project in your Portfolio!
π
You can charge 10-20$ for a simple landing page with a good design. If you are not a designer, just find somebody who will help you with it for some commission on the sales
Ideas for your HTML Website Template:
- Landing page.
- Multi-Purpose Template.
- Coming Soon page.
Using map(), we can iterate through all items in an array, taking actions on each one. The result of this operation is provided as a new array.
2. array.filter()
Using filter(), we can iterate through all items in an array and filter them depending on conditions inside a method. The result of this operation is provided as a new array.