Functions in Javascript
Hi, I am Rasla. Full Stack Developer with 2 years of experiance.In this blog, you will learn a lot about javascript functions.
A JavaScript function is a block of code designed to perform a particular task. A JavaScript function is executed when “something” invokes it (calls it).
Example
JavaScript Function Syntax
A JavaScript function is defined with the function keyword, followed by a name, followed by parentheses (). The parentheses may include parameter names separated by commas:(parameters, parameter2, …). When you call a function, it executes the code inside its own {} curly braces. The parameters are taken by a function are used as a local variable inside a function.
The Return Key Word
The code after the return statement is not reached means not run by javascript. If the function was invoked from a statement, JavaScript will “return” to execute the code after the invoking statement. Functions often compute a return value.
Variables inside a function
If You declare a variable inside a function, it cant be accessed from outside of a function. it’s only accessed inside a function. Not from outside. Local variables are created when a function starts and deleted when the function is completed.
Here, the localSum variable is created inside a function, so it cant be accessed from outside of the function.
Calling a Function
If u log a function without the () operator, it does not invoke the function. it refers to that function object, not its return value. The function is not called until you execute it using the () operator.
Conclusion
Hope that you will learn a lot from this, article. if you do, make sure to share my content. Thanks.