Lecture: Functions in JS
with Carmen Salas • 2024/09/30
Basic Concepts
variables are containers for values (strings, numbers, booleans, etc.) In ES6 (EcmaScript6), we use either let or const.
- letallows variable re-definition
- constdoesn't! Although some types/objects like- Arrayscan work around this to an extent.
- varkeyword is no longer used in modern JS practices but it used to serve both the purposes of- letand- constwith the key difference being that it is NOT block-scoped.
Functions in JavaScript
A JavaScript function is a block of code designed to perform a particular task, and is executed when it is invoked or called.
Typical function syntax:
   function hello (name) {
      return "hello" + name;
   };- function- JS keyword for defining functions
- hello- function name
- name- function parameter
- return "hello" + name;- function body (anything in between the curly braces is considered as such)
Arrow function syntax:
   const hello = (name) => {
      return "hello" + name;
   };- no keyword - function-- hello- is the variable name, and also serves as the function name
- without a name (in this case, - hello) and the- constkeyword, the function becomes anonymous
Console.log vs Return
- console.logis essentially a print function
- returnis used, within a function body, to terminate it and optionally return a value
Parameters vs Arguments
   const hello = (name) => {
      return "hello" + name;
   };
   hello("Motun");- name- function parameter (placeholder for arguments)
- "Motun"- function argument (value passed into a function when it is invoked)
Testing our Code
Just console.log() it.
Last updated