Understanding Functions and Scope in JavaScript: A Beginner's Guide

![[JS 강좌] 2강 - 개발 환경 구축하기 (VS Code, Chrome 개발자 도구)](/uploads/editor/JS-%EA%B0%95%EC%A2%8C-2%EA%B0%95-%EA%B0%9C%EB%B0%9C-%ED%99%98%EA%B2%BD-%EA%B5%AC%EC%B6%95%ED%95%98%EA%B8%B0-VS-Code-Chrome-%EA%B0%9C%EB%B0%9C%EC%9E%90-%EB%8F%84%EA%B5%AC-20260706-0dfe9b.webp)
What is a Function?
When writing programs, you often need to reuse the same code multiple times.
For example, imagine a website with these features:
Login processing
Registration handling
Comment posting
Liking increment
Date display
If you wrote each feature from scratch every time, your code would become long and hard to manage.
This is where functions come in—they let you group specific functionality into reusable blocks of code.
You can call them whenever needed, making your code cleaner and more organized.
Why Use Functions?
The main reason to use functions is reusability.
For instance, if you need to print "Hello" multiple times, writing it repeatedly would look like this:
console.log("Hello");
console.log("Hello");
console.log("Hello");
But with a function, it becomes much simpler:
function hello() {
console.log("Hello");
}
hello();
hello();
hello();
This approach shortens your code and makes it easier to update.
Defining a Function
You create functions using the function keyword.
The basic structure looks like this:
function functionName() {
Code to execute
}
Here's an example:
function welcome() {
console.log("Welcome to the JavaScript course");
}
The function isn't executed yet—it must be called.
Calling a Function
Executing a function is called calling.
function welcome() {
console.log("Welcome");
}
welcome();
Output:
Welcome
You can call the same function multiple times.
welcome();
welcome();
welcome();
Parameters
Functions can accept values as inputs, called parameters.
Here's an example:
function hello(name) {
console.log(name + " is welcome");
}
hello("John");
Output:
John is welcome
You can pass different values to get varied results.
hello("Alice");
hello("Bob");
Output:
Alice is welcome
Bob is welcome
This allows one function to produce different outcomes.
Multiple Parameters
You can use multiple parameters in a function.
function add(a, b) {
console.log(a + b);
}
add(10, 20);
Output:
30
Returns
Functions can return results using the return statement.
function add(a, b) {
return a + b;
}
let result = add(10, 20);
console.log(result);
Output:
30
The return statement lets you store or reuse the function's result.
This is a common practice in real-world development.
Code After Return Is Not Executed
If you use return, the function stops immediately, and any code after it won't run.
function test() {
return;
console.log("This won't execute");
}
test();
The line after return is skipped.
Calling Functions Within Other Functions
You can call one function from within another.
function hello() {
console.log("Hello");
}
function start() {
hello();
}
start();
This structure is common as projects grow larger.
Global Variables
Variables declared outside functions can be accessed anywhere in the program—they're called global variables.
let name = "John";
function hello() {
console.log(name);
}
hello();
Output:
John
Local Variables
Variables created inside a function are only accessible within that function.
function test() {
let age = 20;
console.log(age);
}
test();
But they can't be used outside the function.
console.log(age);
This will cause an error because age is undefined outside the function.
What Is Scope?
Scope defines the range where a variable can be used.
In simple terms, it answers:
"Where can this variable be accessed?"
The two main types are:
Global scope
Local scope
Global Scope
Variables declared outside functions have global scope—they can be used anywhere in the program.
let message = "JavaScript";
function print() {
console.log(message);
}
print();
console.log(message);
Local Scope
Variables declared inside a function have local scope—they're only accessible within that function.
function test() {
let score = 100;
console.log(score);
}
Local variables disappear when the function ends.
What Happens If Variable Names Conflict?
Consider this example:
let name = "John";
function test() {
let name = "Alice";
console.log(name);
}
test();
console.log(name);
Output:
Alice
John
The local variable inside the function takes precedence over global ones.
Arrow Functions
Modern JavaScript often uses arrow functions for concise syntax.
The basic form is:
const hello = () => {
console.log("Hello");
};
With parameters:
const add = (a, b) => {
return a + b;
};
You can even write it more simply:
const add = (a, b) => a + b;
This syntax is widely used in real-world projects.
Practical Examples of Functions
In web development, nearly all features are implemented as functions:
Login
Registration
Comment posting
Search functionality
Liking system
File uploads
Input validation
AJAX requests
The larger the project, the more important well-structured functions become.
Key Takeaways from This Lesson
Today we covered two core JavaScript concepts: functions and scope. Remember these key points:
Functions group specific code for reuse.
They allow you to avoid repeating the same code multiple times.
You can pass different values using parameters.
The
returnstatement lets functions send back results.Variables outside functions are global.
Variables inside functions are local.
Scope defines where variables can be used.
Arrow functions are commonly used in modern JavaScript.
Conclusion
Functions are a fundamental part of learning JavaScript. At first, they may seem like simple code groupings, but in real projects, almost every feature is built using functions.
Mastering functions leads to shorter, cleaner, and more maintainable code.
Try running the examples yourself—call functions multiple times, experiment with parameters, and observe how returns work. This hands-on practice will help solidify your understanding.
In our next lesson, we'll explore arrays and objects, which are essential for efficiently managing collections of data in JavaScript.
FAQ
What is a function in JavaScript?
A function is a reusable block of code designed to perform a specific task. You define it once and call it whenever needed, reducing repetition and improving code organization.
How do I use parameters in functions?
Parameters allow you to pass values into a function. For example, `function greet(name) { console.log('Hello ' + name); }` lets you call `greet('Alice')` for different outputs.
What is the difference between global and local variables?
Global variables are declared outside functions and can be accessed anywhere. Local variables exist only inside a specific function and cannot be used outside it.
Korean Source
This article is an English localization of a Korean-language report from BlogScore. Original: [JS 강좌] 5강 - 함수(Function)와 스코프(Scope) 이해하기