Functions are used to organise the code into separate meaningful blocks to maintain and debug easily.
-
Syntax of functions:
function nameOfFunction(parameters) { //statements } nameOfFunction(arguments) //calling function
-
Function declaration:
function greet(avenger) { return `Hello, ${avenger}` } greet("ironman")
-
Function Expression:
const greet = function(avenger) { return `Hello, ${avenger}` } greet("ironman")
-
Default Parameters:
const greet = function(avenger = "ironman") { return `Hello, ${avenger}` } greet()
-
Functions vs Methods
- Functions does not specific to any objects.
- Methods are specific to objects.