JavaScript FUNCTIONS are easy! (12:14)
Functions are sections of reusable code. Declare code once and use it whenever you want. Learn how to create functions, pass arguments, use parameters, and return values.
- Function declaration and syntax
- Calling/invoking functions
- Parameters and arguments
- Return values and the return keyword
- Function reusability
- Building practical functions
- Ternary operator in functions
From transcript: "A function is a section of reusable code. You declare that code once and you can use it whenever you want. All you have to do is call the function to execute that code."
- ✅ Reusability - Write once, use many times
- ✅ Organization - Keep code structured and readable
- ✅ Maintainability - Update in one place
- ✅ Flexibility - Accept different inputs via parameters
// Function Declaration
function functionName() {
// Code to execute
}
// Function Call
functionName();From transcript: "We'll create a function to sing Happy Birthday. Whenever we call this function it's going to sing Happy Birthday."
function happyBirthday() {
console.log("Happy Birthday to you!!");
console.log("Happy Birthday to you!!");
console.log("Happy Birthday dear you!!");
console.log("Happy Birthday to you!!");
}
// Call the function
happyBirthday();
// Output:
// Happy Birthday to you!!
// Happy Birthday to you!!
// Happy Birthday dear you!!
// Happy Birthday to you!!Reusability in action:
happyBirthday(); // Sings once
happyBirthday(); // Sings again
happyBirthday(); // Sings a third timeFrom transcript: "As many times as I want. In fact that's what a function is - it's a section of reusable code. Reuse it whenever you want. You just have to call it."
From transcript: "You can send a function some values or variables."
- Parameters - Variables listed in function declaration (placeholders)
- Arguments - Actual values passed when calling the function (data)
From transcript: "These are parameters and what you send the function are arguments."
function happyBirthday(username, age) {
console.log("Happy Birthday to you!!");
console.log("Happy Birthday to you!!");
console.log(`Happy Birthday dear ${username}!!`);
console.log("Happy Birthday to you!!");
console.log(`You are ${age} years old!!`);
}
// Call with different arguments
happyBirthday("BroCode", 25);
// Output:
// Happy Birthday to you!!
// Happy Birthday to you!!
// Happy Birthday dear BroCode!!
// Happy Birthday to you!!
// You are 25 years old!!
happyBirthday("SpongeBob", 30);
// Happy Birthday dear SpongeBob!!
// You are 30 years old!!
happyBirthday("Patrick", 35);
// Happy Birthday dear Patrick!!
// You are 35 years old!!From transcript: "That's a benefit of passing arguments to a function. When the function receives this data it can do something with it."
// ❌ Wrong order
function happyBirthday(age, username) {
console.log(`Happy Birthday dear ${username}!!`);
console.log(`You are ${age} years old!!`);
}
happyBirthday("Patrick", 37);
// Output: Happy Birthday dear 37!! You are Patrick years old!!From transcript: "Now the order of the parameters does matter. If I were to switch age and username, here's what happens... With the arguments that you're passing to your function you'll want to be sure that the parameters match up."
From transcript: "We can send some data back to the place in which we call a function, but we need to use this return keyword."
function add(x, y) {
let result = x + y;
return result;
}
// Store returned value
let answer = add(2, 3);
console.log(answer); // 5
// Or use directly
console.log(add(2, 3)); // 5From transcript: "When you return something from a function, after you resolve the function, think of it as becoming whatever is returned. In this case it's our result."
// Long version
function add(x, y) {
let result = x + y;
return result;
}
// Shorthand version
function add(x, y) {
return x + y;
}From transcript: "There is a shortcut too. You don't necessarily need to declare a variable within this function. We could shorten this to return x + y."
function add(x, y) {
return x + y;
}
console.log(add(10, 5)); // 15function subtract(x, y) {
return x - y;
}
console.log(subtract(10, 5)); // 5function multiply(x, y) {
return x * y;
}
console.log(multiply(10, 5)); // 50function divide(x, y) {
return x / y;
}
console.log(divide(10, 5)); // 2From transcript: "We'll create a function to determine if a number is even or odd."
function isEven(number) {
if (number % 2 === 0) {
return true;
}
else {
return false;
}
}
console.log(isEven(10)); // true
console.log(isEven(11)); // false
console.log(isEven(12)); // trueFrom transcript: "If our number modulus 2 - modulus gives you the remainder of any division - if this number divides by two evenly, if this is equal to zero... then let's return the Boolean value true, else we'll return false."
function isEven(number) {
return number % 2 === 0 ? true : false;
}
console.log(isEven(12)); // true
console.log(isEven(13)); // false
console.log(isEven(14)); // trueFrom transcript: "If you would like a shortcut for this you can use the ternary operator."
From transcript: "We'll create a function to see if an email is valid."
function isValidEmail(email) {
if (email.includes("@")) {
return true;
}
else {
return false;
}
}
console.log(isValidEmail("BroCode@gmail.com")); // true
console.log(isValidEmail("ElonMusk.com")); // falseFrom transcript: "We'll check to see if our email contains the at character. If our email includes the at character, then we will return true - that is a valid email. They have the at character. Else we will return false."
function isValidEmail(email) {
return email.includes("@") ? true : false;
}
console.log(isValidEmail("Zuckerberg@meta.com")); // true
console.log(isValidEmail("ElonMusk.com")); // false// Function = A section of reusable code.
// Declare code once, use it whenever you want.
// Call the function to execute that code.
function happyBirthday(username, age) {
console.log("Happy Birthday to you!!");
console.log("Happy Birthday to you!!");
console.log(`Happy Birthday dear ${username}!!`);
console.log("Happy Birthday to you!!");
console.log(`You are ${age} years old!!`);
}
happyBirthday("BroCode", 25);
happyBirthday("SpongeBob", 30);
happyBirthday("Patrick", 35);
function add(x, y) {
return x + y;
}
function subtract(x, y) {
return x - y;
}
function multiply(x, y) {
return x * y;
}
function divide(x, y) {
return x / y;
}
function isEven(number) {
return number % 2 === 0 ? true : false;
}
function isValidEmail(email) {
if (email.includes("@")) {
return true;
}
else {
return false;
}
}
console.log(add(10, 5)); // 15
console.log(subtract(10, 5)); // 5
console.log(multiply(10, 5)); // 50
console.log(divide(10, 5)); // 2
console.log(isEven(40)); // true
console.log(isValidEmail("BroCode@gmail.com")); // true
console.log(isValidEmail("ElonMusk.com")); // falseFrom transcript: "To call a function you type the function name then you add a set of parentheses. I like to think of the parentheses as two telephones talking to each other - that's how I remember it."
functionName(); // () = two telephones 📞📞function functionName(parameter1, parameter2) {
// Code to execute
return value; // Optional
}functionName(argument1, argument2);function greet(name) { // 'name' is a parameter
console.log(`Hello ${name}`);
}
greet("Alice"); // "Alice" is an argumentfunction square(num) {
return num * num; // Sends value back
}
let result = square(5); // result = 25-
Descriptive Names - Use clear, action-based names
// ✅ Good function calculateTotal() { } // ❌ Bad function doStuff() { }
-
Single Responsibility - One function, one task
// ✅ Good - specific purpose function add(x, y) { return x + y; }
-
Return Early - Exit when condition is met
function isValidEmail(email) { if (!email.includes("@")) { return false; } return true; }
-
Consistent Parameters - Keep order logical
function createUser(name, age, email) { // Name first, then age, then email }
From transcript: "That's an introduction to functions - it's a section of reusable code. You declare some code once and use it whenever you want. You call the function to execute that code. If you need to send your function some data, you'll need a matching set of arguments and parameters. And you can return something too with the return keyword."
- Functions = Reusable sections of code
- Declare once, use many times
- Call/invoke a function to execute it
- Parameters = placeholders in declaration
- Arguments = actual values passed when calling
- Parameter order matters
- Return sends data back to caller
- Ternary operator can simplify true/false returns
- Functions make code DRY (Don't Repeat Yourself)
// Create a function that converts Celsius to Fahrenheit
function celsiusToFahrenheit(celsius) {
// Formula: (celsius * 9/5) + 32
// Your code here
}// Create a function that returns the larger of two numbers
function max(a, b) {
// Your code here
}// Create a function that reverses a string
function reverseString(str) {
// Your code here
}// Create a function that returns letter grade based on score
function getGrade(score) {
// 90+ = A, 80-89 = B, 70-79 = C, etc.
// Your code here
}Duration: 12:14
Difficulty: Beginner
Category: Functions & Code Organization