Functions in Javascript (Declaration, Expression, Arrow)

// Function Declaration
function sum(a, b) {
return a + b;
}
const result = sum(3, 7); // calling the function
// Function Expression
let sum = function(a, b) {
return a + b;
};
const result = sum(1, 3) // calling the function
let sum = (a, b) => a + b; // note it does not require a "return" keyword, because it is implicit
let displayA = a => alert("this is ", a);
let sayHi = () => alert("Hello!");
let sum = (a, b) => {
let result = a + b;
return result;
};
let sum = function(a,b) { ... };
// At this point, sum now contains a function.
sayHi("Pillow"); // Hello, Pillow function sayHi(name) { 
alert( `Hello, ${name}` );
}
sayHi("Pillow"); // Error! // so far, the execution has never encountered the sayHi functionlet sayHi = function(name) { // (*)
alert( `Hello, ${name}` );
};
// sayHi can be used from this point forward only
  • Do not have their own this (inherit it from its nearest object-bound parent, read more here)
  • Do not have arguments
  • Can’t be called with new
  • Do not have super

--

--

Frontend developer with master’s degrees in Computer Science and Politics. Passionate about animals, software accessibility and UI/UX design.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Maya Novarini

Maya Novarini

Frontend developer with master’s degrees in Computer Science and Politics. Passionate about animals, software accessibility and UI/UX design.