- 1
What is the work of function in JavaScript
i am now learning it
2 Answers
+ 5
As in any language, "the work of function" is to provide a reusable block of code (instructions/commands) wich you can run whenever you want in another block of code, and able to return a value as the result of computation, through passing to it some values also as parameter...
Imagine you have a program, where you have to repeat computation of distances, you could define a function to get the distance between two points, and call it (run it) each time you need it, without rewritting the lines of code to compute the result...
Functions wich doesn't return values are strictly (but not necessarly usualy) named "procedures" as they "only" do something but doesn't return a result...
Functions attached to an object (in OOP) are strictly (but not necessarly usualy) named "methods"...
"Pure" functions, are functions wich doesn't modify any outside variables values (designed to only write private variables, and only read outer scope ones)...
+ 2
u can read it here
https://www.w3schools.com/js/js_functions.asp