DEV Community

 Areg2004
Areg2004

Posted on

Javascript Functions

A JavaScript function is a block of code designed to perform a particular task. A JavaScript function is executed when "something" calls it. For example,
function Function1(number1, number2) {
return number1 + number2;
}
And this is how we would calculate the sum of number1 and number2. As we just saw the Javascript function is defined by the keyword function. It also uses parentheses. Function names can contain letters, digits, and underscores. The code to be executed, by the function, is placed inside curly brackets. When JavaScript reaches a return statement, the function will stop executing. If the function was invoked from a statement, JavaScript will "return" to execute the code after the invoking statement. Functions are the main “building blocks” of the program. They allow the code to be called many times without repetition.
Function scope is one of the scopes of Javascript which is included in a local scope. Variables defined inside a function are not accessible (visible) from outside the function. Variables declared with var, let and const all have a function scope. Variables defined inside a function cannot be accessed from anywhere outside the function, because the variable is defined only in the scope of the function. A function can have one or more parameters, which will be supplied by the calling code and can be used inside a function. JavaScript is a dynamic type scripting language, so a function parameter can have a value of any data type.

Top comments (0)