MOCKSTACKS
EN
Questions And Answers

More Tutorials









Javascript Functions

Functions in JavaScript provide organized, reusable code to perform a set of actions. Functions simplify the coding process, prevent redundant logic, and make code easier to follow. This topic describes the declaration and utilization of functions, arguments, parameters, return statements and scope in JavaScript.


function foo() {
 var a = 'hello';
 console.log(a); // => 'hello'
}

Nested functions are possible in JavaScript and the same rules apply.


function foo() {
 var a = 'hello';
 function bar() {
 var b = 'world';
 console.log(a); // => 'hello'
 console.log(b); // => 'world'
 }
 console.log(a); // => 'hello'
 console.log(b); // reference error
}

When JavaScript tries to resolve a reference or variable, it starts looking for it in the current scope. If it cannot find that declaration in the current scope, it climbs up one scope to look for it. This process repeats until the declaration has been found. If the JavaScript parser reaches the global scope and still cannot find the reference, a reference error will be thrown.


var a = 'hello';
function foo() {
 var b = 'world';
 function bar() {
 var c = '!!';
 console.log(a); // => 'hello'
 console.log(b); // => 'world'
 console.log(c); // => '!!'
 console.log(d); // reference error
 }
}

This climbing behavior can also mean that one reference may "shadow" over a similarly named reference in the outer scope since it gets seen first.


var a = 'hello';
function foo() {
 var a = 'world';
 function bar() {
 console.log(a); // => 'world'
 }
}

The way JavaScript resolves scoping also applies to the const keyword. Declaring a variable with the const keyword implies that you are not allowed to reassign the value, but declaring it in a function will create a new scope and with that a new variable


function foo() {
 const a = true;
 function bar() {
 const a = false; // different variable
 console.log(a); // false
 }
 const a = false; // SyntaxError
 a = false; // TypeError
 console.log(a); // true
}

However, functions are not the only blocks that create a scope (if you are using let or const). let and const declarations have a scope of the nearest block statement. See here for a more detailed description.



Conclusion

In this page (written and validated by ) you learned about Javascript Functions . What's Next? If you are interested in completing Javascript tutorial, your next topic will be learning about: Javascript Classes.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.