Theoretical recap.
A function in JavaScript is a reusable block of code that performs a task and, optionally, returns a value.
In ES6, you can declare functions with the classic function keyword or with arrow function syntax.
A function usually takes parameters, input placeholders, and receives arguments, actual values, when it is called.
The mental model to keep in mind, a function is a machine.
You feed it inputs, it processes them, and it hands you back an output via the return keyword.
Classic syntax, function greet name, return hello plus name.
Arrow syntax, const greet equals, name, equals greater than hello dollar name dot notice the template literal, backticks, which lets you embed variables directly with dollar, instead of concatenating with plus.
Common mistakes to avoid, forgetting the return statement, the function then returns undefined, confusing console.log inside a function with actually returning a value, mixing up parameters and arguments, forgetting parentheses when calling the function, greet versus greet, and using straight quotes instead of backticks for template literals.
Mini example.
const greet equals first name equals greater than welcome dollar first name.
Console.log greet Sarah slash slash welcome Sarah.
This brings together everything you have already practiced.
Variables, operators, conditions, and now packaging logic into a callable reusable function.
Read the full transcript.
Create an account to read the whole episode, search across every transcript, and follow the shows you care about.
Theoretical recap.
A function in JavaScript is a reusable block of code that performs a task and, optionally, returns a value.
In ES6, you can declare functions with the classic function keyword or with arrow function syntax.
A function usually takes parameters, input placeholders, and receives arguments, actual values, when it is called.
The mental model to keep in mind, a function is a machine.
You feed it inputs, it processes them, and it hands you back an output via the return keyword.
Classic syntax, function greet name, return hello plus name.
Arrow syntax, const greet equals, name, equals greater than hello dollar name dot notice the template literal, backticks, which lets you embed variables directly with dollar, instead of concatenating with plus.
Common mistakes to avoid, forgetting the return statement, the function then returns undefined, confusing console.log inside a function with actually returning a value, mixing up parameters and arguments, forgetting parentheses when calling the function, greet versus greet, and using straight quotes instead of backticks for template literals.
Mini example.
const greet equals first name equals greater than welcome dollar first name.
Console.log greet Sarah slash slash welcome Sarah.
This brings together everything you have already practiced.
Variables, operators, conditions, and now packaging logic into a callable reusable function.