π‘ κΈ°λ³Έ λ¬Έλ²
function ν¨μλͺ
(μ
λ ₯κ°) {
// μνν μΌ
return λ°νκ° // μμ μ
}
β
ν¨μλͺ
(μ
λ ₯κ°);
I. ν¨μλ₯Ό μ¬μ©νλ€λ κ²
1. λ°λ³΅λ μ μλ μμ
μ μ μν΄λλ κ²
// ν¨μ μ¬μ© μ
let a = 3, b = 4;
β
console.log(`${a} + ${b} = ${a + b}`);
console.log(`${a} - ${b} = ${a - b}`);
console.log(`${a} * ${b} = ${a * b}`);
console.log(`${a} / ${b} = ${a / b}`);
β
let c = 10, d = 2;
β
console.log(`${c} + ${d} = ${c + d}`);
console.log(`${c} - ${d} = ${c - d}`);
console.log(`${c} * ${d} = ${c * d}`);
console.log(`${c} / ${d} = ${c / d}`);
β
let e = 7, f = 5;
β
console.log(`${e} + ${f} = ${e + f}`);
console.log(`${e} - ${f} = ${e - f}`);
console.log(`${e} * ${f} = ${e * f}`);
console.log(`${e} / ${f} = ${e / f}`);
function allArithemics (x, y) {
console.log(`${x} + ${y} = ${x + y}`);
console.log(`${x} - ${y} = ${x - y}`);
console.log(`${x} * ${y} = ${x * y}`);
console.log(`${x} / ${y} = ${x / y}`);
}
β
let a = 3, b = 4;
allArithemics(a, b);
β
let c = 10, d = 2;
allArithemics(c, d);
β
let e = 7, f = 5;
allArithemics(e, f);
2. inputμ λ°μ outputμ λ°ν return νλ κ²
function add(x, y) {
return x + y; // βοΈ κ°μ λ°ν
}
β
let z = add(2, 3);
β
console.log(z);
console.log(
add(add(6, 7), add(8, 9))
);
function isOdd (x) {
return !!(x % 2);
}
β
let num = 12;
β
console.log(
`${num}(λ)μ ${
isOdd(num) ? 'ν' : 'μ§'
}μμ
λλ€.`
);
a. inputμΌλ‘ λ°λ κ° - μΈμμ μΈμ
function add(x, y) {
// x, yλ₯Ό μΈμ λλ 맀κ°λ³μ(parameter)λΌ λΆλ¦
return x + y;
}
β
// a, bλ₯Ό μΈμ(argument)λΌ λΆλ¦
let z = add(2, 3);
- μΌλ°μ μΌλ‘λ κ΅³μ΄ κ΅¬λΆνμ§ μκ³ νΌμ©ν΄μ μ¬μ©ν¨
b. κΌ μΈμλ₯Ό λ°κ±°λ κ°μ λ°ννλ κ²μ μλ
let currentTemp = 24.5;
β
function logCurrentTemp () {
console.log(`νμ¬ μ¨λλ μμ¨ ${currentTemp}λμ
λλ€.`);
}
β
console.log('λ°νκ°:', logCurrentTemp());
- return λ¬Έμ΄ μ μλμ΄ μμ§ μμΌλ©΄ undefined λ°ν
- π‘ console.log μ€ν λ€ undefinedκ° λ¨λ μ΄μ
c. βοΈ returnλ¬Έμ κΌ λ§μ§λ§μ
function add (x, y) {
console.log(`${x}μ ${y}λ₯Ό λν©λλ€.`);
return x + y;
console.log(`κ²°κ³Όλ ${x + y}μ
λλ€.`);
}
β
console.log(add(2, 7));
d. π‘ νΈμ΄μ€ν
hoisting
// ν¨μλ μ€νλ¬Έλ³΄λ€ λμ€μ μ μνλ κ²μ΄ κ°λ₯
// λ³μλ μμλ λΆκ°λ₯! (var μ μΈ)
console.log(add(2, 7));
β
function add (x, y) {
return x + y;
}
II. ν¨μλ₯Ό μ μνλ λ°©λ²λ€
1. ν¨μ μ μΈ
function add (x, y) {
return x + y;
}
β
console.log(add(2, 7));
2. μμλ λ³μμ ν¨μ λμ
ν¨μλ κ°
const subt = function (x, y) {
return x - y;
}
β
console.log(subt(7, 2));
function add (x, y) {
return x + y;
}
β
console.log(add(2, 7));
// π‘ κΈ°μ‘΄μ ν¨μλ₯Ό μ¬μ μνλκ²λ κ°λ₯
add = function (x, y) {
console.log(`${x}μ ${y}λ₯Ό λν©λλ€.`);
console.log(`κ²°κ³Όλ ${x + y}μ
λλ€.`);
return x + y;
}
β
console.log(add(2, 7));
3. νμ΄ν ν¨μ
// ν μ€ μμ κ°λ§ λ°νμ
const mult = (x, y) => x * y;
β
console.log(mult(2, 7));
// λ μ€ μ΄μμ μμ
μ΄ μμ μ
const mult = (x, y) => {
console.log(`${x}μ ${y}λ₯Ό κ³±ν©λλ€.`);
console.log(`κ²°κ³Όλ ${x * y}μ
λλ€.`);
return x * y;
};
β
console.log(mult(2, 7));
// μΈμκ° νλμΌ λλ κ΄νΈ μμ΄ μ μΈ κ°λ₯
const pow = x => x ** 2;
console.log(pow(3));
- β οΈ νμ΄ν ν¨μλ function μ μΈ ν¨μμ κΈ°λ₯ μ°¨μ΄κ° μμ μ΄ν λ€λ¦
β οΈ 2λ²κ³Ό 3λ² λ°©λ²μΌλ‘ μ μΈν ν¨μλ νΈμ΄μ€ν
λμ§ μμ
console.log(div(8, 4));
β
const div = function (x, y) {
return x / y;
}
console.log(div(8, 4));
β
const div = (x, y) => x / y;
π‘ ν¨μ μμ± μμ μ΄ λ€λ₯΄κΈ° λλ¬Έ
- 1λ² λ°©λ²μΌλ‘ μ μλ ν¨μλ μμ§μ μ½λ μ€ν μ΄μ 미리 μμ±λ¨