Loading, please wait...

What is a JavaScript function?

May 22, 2020 Js, JavaScript, js functions, 1530 Views
What is a JavaScript function? ⁣

A JavaScript function is a block of code designed to perform a particular task. ⁣ ⁣
⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⁣
❌When does a Javascript function runs? ⁣
A JavaScript function is executed when "something" invokes it (calls it). ⁣ ⁣
⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⁣
❌How do we call a JavaScript function? ⁣
We call a JavaScript function by calling it’s named followed by the call signature. ⁣ ⁣
⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⁣
👉🏼Let's write a function that returns a string that says "Hello World". ⁣
We can declare this function in two ways: ⁣ ⁣
⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⁣
🤯ES5 declaration:

function helloWorld(){ ⁣
return 'Hello World'; ⁣
} ⁣

 ⁣🤯ES6 declaration: ⁣ ⁣⠀⠀⠀⠀⠀ ⁣

const helloWorld = () => { ⁣
return 'Hello World'l ⁣
} 

👉🏼To invoke our function we will write the name/keyword of out function followed by its signature like this: ⁣ ⁣
⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⁣
helloWorld(); // Hello World ⁣ ⁣
⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⁣
Congratulations, you just wrote your first JavaScript function!! 🎉🎉 ⁣ ⁣
⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⁣
💭What was the first language you wrote "Hello World" on? ⁣ ⁣