Javascript functions, how can I start to understand them?

I fully understand that in order to learn javascript, I need to know how functions work, I understand the basics of passing parameters, and then I call a function with values ​​to possibly add something together, etc. I read countless articles about functions, as well as books, etc., but I just don’t understand how they are used and when they should be used, etc., more advanced functions that have maybe 4 parameters, and they do different calculations and return different values ​​that are fired back into the script just bothers me.

What I would like to know, first of all, how can I overcome this confusion, as well as any words of wisdom that you may have? I will also add that I have no prior programming experience, and for the last 2 months I often hit my head against a brick wall, as I just can't understand javascript.

+6
source share
6 answers

Functions in programming are similar to functions in mathematics. They make a certain contribution and produce a result (well, sometimes they don’t do it, but they do something). They can organize your code, encapsulate functionality and avoid writing the same code in different places ( DRY ).

Functions that take more parameters are not necessarily more complex.

Take a look

+8
source

Functions provide a way to segment repeating code snippets so you don't have to repeat the same functionality over and over.

If you are after a non-reprogrammed analogy, you can think about it in business terms:

Imagine you need to print, collate, punch, and link a document. The first time you do it yourself. Then next time you have to do it again ... and again ... So what can you do? You can hire an assistant administrator (call him Bill) to do this for you. Then, the next time you have to print, match, remove holes and bind a document, you can just tell Bill that the document is for printing (one option) and he will do it for you and return the document to you when he is done.

In this case, Bill performs a function that does some work and returns something (or at least an example of one!)

Now imagine that you want to send an email to someone to pursue a payment. Of course, you could type a letter and publish it yourself, but wouldn't it be easier if someone else did it for you? Let her call Jane. You can tell Jane which company should send the letter and how much they should (parameters), and she will leave, type it and send it. You do not need to know if she did this or not, because you trust her to do her job.

In this case, Jane is a function that returns nothing, but still does some work.

+6
source

There are two things that you should understand in my opinion:

  • DOM structure that is actually managed using JavaScript
  • JavaScript language (or generally programming)

I would choose a basic tutorial like this: http://www.lynda.com/JavaScript-tutorials/Essential-Training-2011/81266-2.html

And the most important part is not just reading, many examples and exercises. If you are not coding, you cannot hang it.

+2
source

IMO besides a high-level understanding of functions, how they are implemented and how they work, it is important to understand the basics of low-level programming (javascript).

It doesn't really matter if you decide to find out with javascript, since the idea is if if () {} / else {}, for (), etc. basically the same everywhere. This is much easier to understand, since you really will know what they do when you look at the code, instead of taking the developer’s word about what he does.

+1
source

Start by reading “JavaScript: the good parts” and watching Doug Crockford’s videos at the YUI Theater.

Then continue to write code.

0
source

Functions are mainly used to prevent code duplication and simplify repetitive tasks. They also help to compress the code you write and make it more readable.

For me, your question seems like you are standing in front of a bunch of LEGO (javascript / programming) and asking what to do with red bricks (functions).

It's best to find something that you want to do with LEGO. Look for simple things that other people have done (study guides) and find what you want to do. Then try to do this and see how the red bricks enter it.

As others have mentioned, the best way is to practice it, try yourself, achieve your goal.

0
source

Source: https://habr.com/ru/post/893672/


All Articles