- Functions allow reusing code by grouping statements.
- Defined with function keyword:
function greet() { console.log("Hello"); } - Calling executes the code:
greet(); // logs "Hello" - Can accept parameters:
function sum(a, b) { return a + b; }
let total = sum(10, 20); //total = 30 - Parameters act as function variables.
- Return keyword sends back a value.
- Scope:
- Functions have local scope
- Variables inside not accessible globally
- Some built-in functions:
- Number/String functions
- encodeURI()
- setTimeout()
- Math functions
- User defined vs built-in functions
- Function expressions and arrow functions
- Explain calling context, hosting