What is a Function in JavaScript? | Function Examples & Guide

Functions in JavaScript. Learn what functions are in JavaScript with examples. This beginner-friendly guide covers function declarations, parameters, return values, and more.
✅ Function Declaration and Function Call
Here’s how you define (declare) a simple function and then call it:
function sayHello() {
console.log('Hello, Sagar!')
}
sayHello() // Function call
Explanation:
function sayHello()
is the function declaration.sayHello();
is the function call.
🧠 Functions with Parameters
You can also make your functions dynamic by using parameters.
JavaScript
function sayHello(name) {
console.log('Hello, ' + name)
}
sayHello('Sagar') // Output: Hello, Sagar
sayHello('John') // Output: Hello, John
Why use parameters?
Parameters allow you to pass data into functions, making them reusable for different values.
➕ Function Example: Adding Two Numbers
Let’s look at another example where a function takes two parameters and adds them.
JavaScript
function sum(a, b) {
let add = a + b
console.log(add)
}
let x = 10
let y = 20
let z = 30
sum(x, y) // Output: 30
sum(y, z) // Output: 50
🔁 Returning Values from Functions
Functions can also return values using the return
statement. This allows the result to be stored or used elsewhere in your code.
JavaScript
function square(x) {
let result = x * x
return result
}
let value = square(-3)
console.log(value) // Output: 9
🧠 Why return values?
Returning values makes your functions more flexible and allows you to store results for later use or use them in expressions.
🗳️ Function Example: Check Voting Eligibility
Here’s a real-world example that checks if a person is eligible to vote based on their age.
JavaScript
function eligibleToVote(age) {
if (age < 0) {
console.log('Invalid age')
} else if (age >= 18) {
console.log('User is eligible to vote')
} else {
console.log('User is not eligible to vote')
}
}
eligibleToVote(0) // Output: Invalid age
eligibleToVote(5) // Output: User is not eligible to vote
eligibleToVote(17.7) // Output: User is not eligible to vote
eligibleToVote(19) // Output: User is eligible to vote
✅ Practical Use Case:
Great for validating user input in form fields before submission.
🔢 Function Example: Check Even or Odd Number
This function determines whether a given number is even or odd.
JavaScript
function evenOrOdd(num) {
if (num % 2 === 0) {
console.log('Even number')
} else {
console.log('Odd number')
}
}
evenOrOdd(2) // Output: Even number
evenOrOdd(3) // Output: Odd number
evenOrOdd(10) // Output: Even number
evenOrOdd(17) // Output: Odd number
💡 When to Use:
This logic is commonly used in loops, conditions, and validations.
📝 Conclusion
JavaScript functions are essential for writing clean, reusable, and modular code. In this tutorial, you learned:
What a function is
How to declare and call functions
Using parameters to make functions dynamic
Returning values from functions
By mastering functions, you lay the foundation for writing more efficient and powerful JavaScript programs.
📚 Also Learn and Explore...
JavaScript function tutorial
What is a function in JavaScript
How to declare functions in JavaScript
JavaScript function examples
Function with parameters in JavaScript
Returning values from JavaScript functions
Happy coding!
What is JavaScript? A Complete Beginner's Guide
Learn what JavaScript is, how it works, and how to add it to your web pages with inline, external, async, and defer script loading strategies.
Read Full StoryJavaScript Loops Explained with Examples | For Loop
Learn how loops work in JavaScript with beginner-friendly examples. This complete guide covers for loops, while loops, and practical use cases like printing messages, iterating arrays, and filtering even/odd numbers.
Read Full Story📘TypeScript: Day 1 – Variables, Types & Functions
TypeScript: Variables, Types & Functions
Read Full StoryWhat is TypeScript?
TypeScript is a superset of JavaScript that adds static typing to the language. This means you can define the type of a variable, function parameter, or object property, allowing the compiler to catch type-related errors before your code runs. This leads to more robust and maintainable code.
Read Full Story