DEV Community

Taguhi Manukyan
Taguhi Manukyan

Posted on

Functions in Java Script

Let's start with the general understanding.

What is a function in general?
A function is a group of code that performs a specific task. When you define a function, you can call it everywhere and do a particular action.

Why do people create functions?

1) Functions Encapsulate Tasks - often functions named "black boxes" because when we call them, we do not care what the inner code of it is. We need just its result.

2)Functions Separate Tasks - programmers can cleanly divide their code into segments to work with it efficiently.

3)Functions Let You Reuse Code - often, we reuse the same code multiple times, and not to repeat ourselves, we can write functions once and call them when needed.

4)Functions Make Testing and Debugging Easier - often, we make mistakes in our code, and it is easier to correct errors in one place and make sure it works appropriately and then call when needed.

Java Script functions

In JavaScript function is a block of code to perform a task, and we can use it when we call it and input arguments if needed.

Function Declaration

To create a function, we have to.

1) write "function" keyword

2) give a name to our function
naming a function has the same rules as naming variables.

 a)Names can contain letters, digits, underscores, and 
 dollar signs.

 b)Names must begin with a letter.

 c)Names can also begin with '$' and '_.'
Enter fullscreen mode Exit fullscreen mode

3) write parenthesis "()"
4) open curly brackets "{}"
5) write the logic of a code
Image description
6)return object which we want to get as output from the function
When the code reaches the command 'return,' it automatically stops execution and returns the variable's value.
Image description

Functions can also be defined as regular variables
1) keyword 'let,' 'var,' or 'const.'
2) function's name
3) equal sign '='
4) function()
5) curly brackets '{}.'
6) logic of the function

Image description

Function Invocation

We write functions, but we have to call them with arguments (if needed) to get the value from the function.

For calling.
1) we write the function's name
2)parenthesis "()"
3)in parenthesis, we need to write arguments(if needed)

Image description

The "()" Operator Invokes the Function. If we call the function without it, JS will return the function itself

Image description

Function Scope

When we define variables inside the function (it does not matter with var, let, or const), variables are accessible only inside the function

Image description

Global variables
Variables that are defined outside of any function become global and can be accessed by any function

Image description

Function arguments

Function arguments can be used inside the function and are local to it.

Checking for variables

Function first searches the logic values inside the function's scope and uses them. If the function does not find, it goes up from its scope and uses the first value of the particular variable.

Image description

Nested functions

Functions have access to the global scope, and we can use functions to plug in other ones.

Image description

Image description

Top comments (0)